【问题标题】:How to mock specific function in object using Jest?如何使用 Jest 模拟对象中的特定函数?
【发布时间】:2015-04-20 02:09:47
【问题描述】:

我正在使用 Jest 测试一个 React/Reflux 应用程序。我在商店里有以下功能:

onLoad: function() {
  console.log("ORIGINAL LOAD");
  // http request here
}

我正在尝试模拟它,以便它只做它需要做的事情而不做实际的网络工作:

beforeEach(function() {

  // mock out onLoad so instead of making API call the store gets test data
  PostStore.onLoad = jest.genMockFunction().mockImplementation(function () {
    var p1 = new Post(
      "54da7df5119025513400000a",                    // id
      "Test Post",                                   // title
      "Kji6ftLjUqhElgnqOBqMUKxYONpU7nK/cu6jTA==\n",  // owner anonId
      "Test Course 1",                               // course name
      "This is a test!",                             // content
      6,                                             // upvotes
      2,                                             // downvotes
      ["Kji6ftLjUqhElgnqOBqMUKxYONpU7nK/cu6jTA==\n"] // voter anonIds
    );

    this.posts = [p1];
    console.log("mocked function");
  });

  // component initialized here
});

然而,模拟函数似乎从未被创建。当我运行测试时,控制台仍然记录ORIGINAL LOAD

什么是覆盖对象方法的正确方法,以便通过执行 ajax 调用而不是在 PostStore 中设置 posts 数组,而是使用测试数据设置它?

【问题讨论】:

  • 您解决了这个问题吗?我有一个非常相似的问题,但我正在使用 Jasmine 进行测试并使用 Rewire 注入依赖项。我在 Reflux GitHub 存储库上记录了一个问题:github.com/spoike/refluxjs/issues/300
  • 我从来没有解决过,但是对于Jasmine,我建议使用Sinon来模拟。
  • 谢谢,原来是 Rewire 的限制,在加载 Reflux 的 store mixin 后注入依赖。我最终选择了inject-loader for webpack
  • 在测试中实际使用 PostStore 之前,您是否在执行 var PostStore = require('PostStore') ?我相信你必须在 require 语句之后定义你的 mockImplementation 以确保开玩笑的自动模拟不会覆盖你的 mockImplementation。
  • 我不确定我是不是。我只用 rails 重写了它。

标签: mocking tdd reactjs jestjs refluxjs


【解决方案1】:

我找到了 jest mock 实例函数 It's here

例如:

import expect from 'expect';

jest.mock('../libs/utils/Master');
import Master from '../libs/utils/Master';

Master.mockImplementation(() => {
  return {
    returnOne: jest.fn().mockReturnValueOnce(1)
  }
})
describe('test Master', function() {
  it('test search', function() {
    let master = new Master();
    expect(master.returnOne()).toEqual(1);
  });
});

【讨论】:

    【解决方案2】:

    差不多了,你需要做的就是

    const onLoad = jest.fn().mockImplementation(function () {
        var p1 = new Post();//Add your stuff here
        this.posts = [p1];
        console.log("mocked function");
      });
    PostStore.onLoad = onLoad.bind(PostStore); //PostStore is your object.
    

    【讨论】:

      猜你喜欢
      • 2017-09-27
      • 1970-01-01
      • 1970-01-01
      • 2020-04-24
      • 2019-05-25
      • 1970-01-01
      • 2019-10-13
      • 2017-12-20
      相关资源
      最近更新 更多