茉莉花(应创建)失败,组件应创建FAILED TypeError:无法读取未定义的属性“ next”

时间:2019-10-11 13:22:05

标签: angular jasmine observable ngrx-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());
  }

}

测试看起来像这样

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();
  });

测试输出给我

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)

如果我删除'expect(component).toBeTruthy();'应该创建的部分还是会失败。在其中使用“下一个”的情况下,它执行什么样的过程?以及如何找到调用的内容?

我是测试和存储的新手,所以对我来说意义不大。感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

这是由于错误导入异步引起的。 我从“ q”更改了导入{异步};从“ @ angular / core / testing”导入{async};