注入后不能修改mock

时间:2017-09-01 15:29:11

标签: angular typescript jasmine karma-jasmine

一旦注入模拟,我怎么能修改它?

describe('MyComponent', () => {
  let component: MyComponent;
  let fixture: ComponentFixture<MyComponent>;
  let mockService: any;

  beforeEach(async(() => {
    mockService = { value: 'default value' };

    TestBed.configureTestingModule({
      declarations: [MyComponent],
      providers: [{ provide: MyService, useValue: mockService }]
    })
      .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(MyComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should do something if  myService has special value', () => {
    mockService.someValue = 'special value';
    // inside my component, this.myService.value still has 'default value' !
  });
});

由于mockService是对象的引用,因此在注入后不能改变它吗?

0 个答案:

没有答案