【问题标题】:jest (Angular): change value of spyon().mockReturnValue开玩笑(Angular):改变 spyon().mockReturnValue 的值
【发布时间】:2019-10-28 20:26:29
【问题描述】:

在两个不同的测试中,我想模拟给定服务中函数的两个不同值。我用:

    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'。我也试过mockClearmockResetmockRestore,但它们似乎都没有做任何事情。我总是得到'foo'。

我应该怎么做才能在第二次测试中获得不同的值?

我有以下版本的jest

    "jest": "^24.1.0",
    "jest-junit": "^6.3.0",
    "jest-preset-angular": "^6.0.2",

我无法更新jest-preset-angular,因为我有this non-solved problem。 :-(

这里的代码有点扩展:

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

【问题讨论】:

标签: angular jestjs


【解决方案1】:

如果您在一个单独的函数中创建您的组件,并带有一个将在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();
  }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-14
    • 2021-12-10
    • 2020-04-09
    • 2020-08-11
    • 2019-05-24
    • 1970-01-01
    • 2019-01-08
    • 2020-02-11
    相关资源
    最近更新 更多