我可以使用Jest使用特定参数模拟函数吗?

Níc*_*sen 28 javascript unit-testing jestjs

我想用Jest模拟一个函数,但只有在使用特定参数调用它时,例如:

function sum(x, y) {
  return x + y;
}

// mock sum(1, 1) to return 4
sum(1, 1) // returns 4 (mocked)
sum(1, 2) // returns 3 (not mocked)
Run Code Online (Sandbox Code Playgroud)

Ruby的RSpec库中实现了类似的功能:

class Math
  def self.sum(x, y)
    return x + y
  end
end

allow(Math).to receive(:sum).with(1, 1).and_return(4)
Math.sum(1, 1) # returns 4 (mocked)
Math.sum(1, 2) # returns 3 (not mocked)
Run Code Online (Sandbox Code Playgroud)

我在测试中想要实现的是更好的解耦,假设我想测试一个依赖于的函数sum:

function sum2(x) {
  return sum(x, 2);
}

// I don't want to depend on the sum implementation in my tests, 
// so I would like to mock sum(1, 2) to be "anything I want", 
// and so be able to test:

expect(sum2(1)).toBe("anything I want");

// If this test passes, I've the guarantee that sum2(x) is returning
// sum(x, 2), but I don't have to know what sum(x, 2) should return
Run Code Online (Sandbox Code Playgroud)

我知道有一种方法可以通过以下方式实现:

sum = jest.fn(function (x, y) {
  if (x === 1 && y === 2) {
    return "anything I want";
  } else {
    return sum(x, y);
  }
});

expect(sum2(1)).toBe("anything I want");
Run Code Online (Sandbox Code Playgroud)

但如果我们有一些糖功能来简化它会很好.

听起来合理吗?我们在Jest中已经有这个功能吗?

感谢您的反馈意见.

STe*_*ary 22

我找到了我的同事最近写的这个图书馆: jest-when

import { when } from 'jest-when';

const fn = jest.fn();
when(fn).calledWith(1).mockReturnValue('yay!');

const result = fn(1);
expect(result).toEqual('yay!');
Run Code Online (Sandbox Code Playgroud)

这是图书馆:https://github.com/timkindberg/jest-when


And*_*nti 22

jest-when提到的STeve Shary可能是最好的选择。

如果你不想安装新的库,当你不关心原来的功能时,这里有一个单行解决方案:

sum.mockImplementation((x, y) => x === 1 && y === 2 && "my-return-value")
Run Code Online (Sandbox Code Playgroud)

  • 感谢您提供不需要外部库的替代方案。 (4认同)
  • 这是测试的一部分,以确保传递正确的参数 (4认同)
  • 但这样你就无法对争论设定期望 (2认同)

Luk*_*gan 19

我认为我也需要一种用参数来模拟的方法,但对我来说,我可以通过简单地知道调用顺序来解决我的问题。

取自笑话文档

const filterTestFn = jest.fn();

// Make the mock return `true` for the first call,
// and `false` for the second call
filterTestFn.mockReturnValueOnce(true).mockReturnValueOnce(false);
Run Code Online (Sandbox Code Playgroud)

因此,在上面的示例中,只要您知道函数在第一次传递时应返回 true,而在第二次传递时应返回 false,那么您就可以开始了!

  • 我认为这种测试很脆弱,因为它取决于非常具体的实现。如果调用的顺序在实现中并不重要,我不会建议使用这种方法。 (15认同)
  • 不是针对特定的问题,但如果你用谷歌搜索“我可以使用 Jest 模拟带有特定参数的函数吗”这个问题就会出现,我的回答只是指出你可能不需要你认为你需要的东西。 (4认同)
  • 不,但此解决方案适用于调用顺序“很重要”的场景。 (2认同)

pea*_*utz 19

import * as helper from "../helper";  //file where all functions are

jest.spyOn(helper, "function_name").mockImplementation((argument) => {

// This argument is the one passed to the function you are mocking

  if (argument === "something") {
    return "something"
  } else {
    return "something else"
  }
});
Run Code Online (Sandbox Code Playgroud)


And*_*rle 9

没有办法在Jest中做到这一点.你可以使用sinons存根.来自文档:

stub.withArgs(arg1 [,arg2,...]);

仅为提供的参数存储方法.这对于在断言中更具表现力很有用,您可以使用相同的调用访问间谍.创建一个可以不同方式响应不同参数的存根也很有用.

"test should stub method differently based on arguments": function () {
    var callback = sinon.stub();
    callback.withArgs(42).returns(1);
    callback.withArgs(1).throws("TypeError");

    callback(); // No return value, no exception
    callback(42); // Returns 1
    callback(1); // Throws TypeError
}
Run Code Online (Sandbox Code Playgroud)