在两个不同的测试中,我想模拟给定服务中函数的两个不同值。我使用:
service = TestBed.get(someService);
spyObj = jest.spyOn(service, 'someFunction').mockReturnValue(of('foo'));
,第一个测试运行良好。那如果我写
spyObj.mockReturnValue(of('second foo'));
或
spyObj = jest.spyOn(someService, 'someMethod').mockReturnValue(of('foo'));
在第二个测试中,我得到的值仍然是'foo'。我也尝试了mockClear
,mockReset
和mockRestore
,但它们似乎都没有任何作用。我总是得到'foo'。
我应该怎么做才能在第二次测试中获得不同的价值?
我有以下jest
版本:
"jest": "^24.1.0",
"jest-junit": "^6.3.0",
"jest-preset-angular": "^6.0.2",
我有this non-solved problem,因此我无法更新jest-preset-angular
。 :-(
此处的代码进行了扩展:
describe('whatever', () => {
let component: SomeComponent;
let fixture: ComponentFixture<SomeComponent>;
let someService: SomeService;
let spyObj: jest.Mock<Observable<any>, [string | string[], object?]>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
SomeModule.forRoot()
],
declarations: [ SomeComponent ],
providers: [ SomeService ]
})
.compileComponents();
}));
beforeEach(() => {
someService = TestBed.get(SomeService);
spyObj = jest.spyOn(someService, 'someMethod').mockReturnValue(of('foo'));
fixture = TestBed.createComponent(SomeComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
afterEach(() => {
fixture.destroy();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should check someFunction when someService returns "foo"', () => {
component.someFunction(); // This function uses the value of someService
// Debugging inside someFunction I get "foo"
expect(component.something).toEqual('foo');
});
it('should check someFunction when someService returns "second foo"', () => {
spyObj.mockReturnValue(of('second foo'));
// this does not work either:
// spyObj = jest.spyOn(someService, 'someMethod').mockReturnValue(of('second foo'));
component.someFunction(); // This function uses the value of someService
// Debugging inside someFunction I still get "foo"
expect(component.something).toEqual('second foo');
});
});
答案 0 :(得分:0)
如果您在单独的函数中创建一个带有将在mockReturnValue
中使用的参数的组件,则该解决方案有效。但是,我不接受此答案,因为我仍然不明白为什么mockReturnValue
不能如预期那样被覆盖,我希望有人仍然可以澄清它。
以下代码有效:
describe('whatever', () => {
let component: SomeComponent;
let fixture: ComponentFixture<SomeComponent>;
let someService: SomeService;
let spyObj: jest.Mock<Observable<any>, [string | string[], object?]>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
SomeModule.forRoot()
],
declarations: [ SomeComponent ],
providers: [ SomeService ]
})
.compileComponents();
}));
afterEach(() => {
fixture.destroy();
});
it('should create', () => {
customBeforeEach();
expect(component).toBeTruthy();
});
it('should check someFunction when someService returns "foo"', () => {
customBeforeEach();
component.someFunction();
expect(component.something).toEqual('foo');
});
it('should check someFunction when someService returns "second foo"', () => {
customBeforeEach('second foo');
component.someFunction();
expect(component.something).toEqual('second foo');
});
function customBeforeEach(value = 'foo'): void {
someService = TestBed.get(SomeService);
spyObj = jest.spyOn(someService, 'someMethod').mockReturnValue(of(value));
fixture = TestBed.createComponent(SomeComponent);
component = fixture.componentInstance;
fixture.detectChanges();
}
});