简体   繁体   English

Jasmine 它(“应该创建”)失败,组件应该创建失败类型错误:无法读取未定义的属性“下一个”

[英]Jasmine it('should create') fails with Component should create FAILED TypeError: Cannot read property 'next' of undefined

my component gets data from store我的组件从商店获取数据

@Component({
  selector: 'iomt-user',
  templateUrl: './user.component.html',
  styleUrls: ['./user.component.scss']
})
export class UserComponent implements OnInit {

  users: Observable<User[]>;

  constructor(readonly store: Store<AppState>) { 
    this.users = this.store.pipe(select(getUsers))
  }

  ngOnInit(): void {
    this.store.dispatch(new LoadUsers());
  }

}

and test looks like this和测试看起来像这样

describe('UserComponent', () => {
  let component: UserComponent;
  let fixture: ComponentFixture<UserComponent>;
  const initialState = { loggedIn: false };

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [UserComponent, UserTableComponent],
      imports: [MaterialModule, FormsModule, RouterTestingModule, UserDomainModule,
        HttpClientTestingModule, ReactiveFormsModule, BrowserAnimationsModule, StoreModule.forRoot(userReducer)],
      providers: [
          provideMockStore({initialState}),
      ]
    }).compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(UserComponent);
    component = fixture.componentInstance;
    component.users = component.store.pipe(select(getUsers));
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });

testing output gives me测试 output 给了我

Chrome 77.0.3865 (Windows 10.0.0) UserComponent should create FAILED
        TypeError: Cannot read property 'next' of undefined
            at <Jasmine>
            at Function.continuer (http://localhost:9876/_karma_webpack_/node_modules/q/q.js:1278:1)
            at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/node_modules/q/q.js:1305:1)
            at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:359:1)
            at ProxyZoneSpec.push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:308:1)
            at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:358:1)
            at Zone.run (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:124:1)
            at runInTestZone (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:561:1)
            at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:576:1)
            at <Jasmine>
Chrome 77.0.3865 (Windows 10.0.0): Executed 20 of 31 (1 FAILED) (0 secs / 5.334 secs)
Chrome 77.0.3865 (Windows 10.0.0) UserComponent should create FAILED
        TypeError: Cannot read property 'next' of undefined
            at <Jasmine>
            at Function.continuer (http://localhost:9876/_karma_webpack_/node_modules/q/q.js:1278:1)
            at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/node_modules/q/q.js:1305:1)
            at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:359:1)
            at ProxyZoneSpec.push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:308:1)
            at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:358:1)
            at Zone.run (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:124:1)
            at runInTestZone (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:561:1)
            at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:576:ne1)

If I remove 'expect(component).toBeTruthy();'如果我删除 'expect(component).toBeTruthy();' the should create part fails anyway.无论如何,应该创建的部分都会失败。 What kind of a process does it do in there that 'next' is used?它在那里使用“下一个”进行什么样的过程? And how can I locate what is the called on?我怎样才能找到被调用的内容?

I am new to testing and to store so that just does not make much sense to me.我是测试和存储的新手,所以这对我来说没有多大意义。 I appreciate any help.我很感激任何帮助。

It was caused because of a wrong import of async.这是由于异步导入错误引起的。 I changed import { async } from "q";我将 import { async } from "q"; to import { async } from "@angular/core/testing";从“@angular/core/testing”导入 { async };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 单元测试应该创建 FAILED TypeError:无法读取未定义的属性(读取“首选项”) - Angular Unit Test should create FAILED TypeError: Cannot read properties of undefined (reading 'preferences') 类型错误:无法读取 jasmine 中未定义的属性“客户端” - TypeError: Cannot read property 'client' of undefined in jasmine 错误类型错误:无法读取 SafeSubscriber.settingsService.create.subscribe.loading [as _next] 处未定义的属性“重置” - ERROR TypeError: Cannot read property 'reset' of undefined at SafeSubscriber.settingsService.create.subscribe.loading [as _next] 使用 Jasmine 进行测试,给出失败:未捕获(承诺):类型错误:无法读取未定义的属性“firstName” - Testing with Jasmine, gives a Failed: Uncaught (in promise): TypeError: Cannot read property 'firstName' of undefined TypeError:无法读取未定义的 BehaviorSubject 的属性“next” - TypeError: Cannot read property 'next' of undefined BehaviorSubject TypeError:创建放大时无法读取未定义的属性“projectPath” - TypeError: Cannot read property 'projectPath' of undefined when create amplify TypeError:无法读取未定义的茉莉花业力的属性“ fetchData” - TypeError: Cannot read property 'fetchData' of undefined jasmine-karma Jasmine 测试错误 - TypeError:无法读取未定义的属性“隐藏” - Jasmine test error - TypeError: Cannot read property 'hide' of undefined Angular/jasmine:TypeError:无法读取未定义的 Angular 的属性“标签” - Angular/jasmine : TypeError: Cannot read property 'labels' of undefined Angular 类型错误:无法读取 jasmine 单元测试用例中未定义的属性“nativeElement” - TypeError: Cannot read property 'nativeElement' of undefined in jasmine unit test case
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM