简体   繁体   English

Angular2单元测试-测试主题(观看服务中的变量)

[英]Angular2 Unit Tests - Test Subject ( watch on variable from service)

I have a watch in one of my components which watches a variable in a service (so a Subject). 我在其中一个组件中有一个手表,可以监视服务中的变量(因此可以监视主题)。 (I got the implementation idea of this here: updating variable changes in components from a service with angular2. However, now my whole tests in the component are failing because I need to mock this somehow but I don't know how and I could not find any answer to that. That's my code: (我在这里有了实现的想法: 使用angular2从服务中更新组件中的变量更改。但是,现在我在组件中的整个测试都失败了,因为我需要以某种方式进行模拟,但是我不知道怎么做,我不能找到任何答案,这是我的代码:

Component where I watch the change of the variable in the constructor: 我在构造器中观察变量变化的组件:

  constructor(private updateService: UpdateService) {

    // check if intents are updated in the updateService through upload component, if yes update training status
    this.intentsSubscription = updateService.intentsChange.subscribe((value) => this.initTrainingStatus());
  }

Service where the Variable-Change is triggered: 触发变量更改的服务:

 public intentsChange: Subject<object> = new Subject<object>();
   .....

    public updateIntents(appId: number) {
        this.requestsService.getAllIntents(appId)
          .subscribe(intents => {
            this.setIntents(intents);
            this.intentsChange.next(intents);
          });
      }

Tests of the component which are currently failing because: 当前由于以下原因而导致组件失败的测试:

TypeError: updateService.intentsChange.subscribe is not a function TypeError:updateService.intentsChange.subscribe不是函数

describe('TrainButtonComponent', () => {
  let component: TrainButtonComponent;
  let fixture: ComponentFixture<TrainButtonComponent>;

  let mockUpdateService;
  let intentsChange: Subject<object> = new Subject<object>();


  beforeEach(async(() => {
      intentsChange() {
        return Subject.of({});
      }
    };
    TestBed.configureTestingModule({
      imports: [ ],
      declarations: [ TrainButtonComponent ],
      providers: [
        { provide: UpdateService, useValue: mockUpdateService }
      ]
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(TrainButtonComponent);
    component = fixture.componentInstance;
    mockUpdateService = fixture.debugElement.injector.get(UpdateService);
    fixture.detectChanges();
  });

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

});

Does anyone know how to fix that Error so my Tests are actually running through again? 有人知道如何解决该错误,以便我的测试再次真正通过吗?

intentsChange what you declared in beforeEach do nothing. intentsChange您在beforeEach声明的内容。

All what you need is to correctly mock updateService in before each: 您所需要做的就是在每个方法之前正确模拟updateService

updateService = TestBed.get(UpdateService); // or somehow else, as you wish
spyOn(updateService, 'intentsChange').and.returnValue(Observable.of(<SomeValueHere>)); // <-- here is main point

Now check that this.initTrainingStatus() was called. 现在检查是否已调用this.initTrainingStatus() This will work. 这将起作用。

Don't forget to import 'rxjs/add/observable/of' somewhere at the top. 不要忘记在顶部import 'rxjs/add/observable/of'某个位置import 'rxjs/add/observable/of'

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM