一旦注入模拟,我怎么能修改它?
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
是对象的引用,因此在注入后不能改变它吗?