13

我对监视茉莉花有点困惑。我有这样的代码,但我不知道如何测试它。

var params = {
    param1: "",
    param2: "link",
    param3: "1", 
    param4 : "1"
};
var func = new myFunction(params );
func.doSomething();

如何测试 func.doSomething 已被调用。

这是我到目前为止写的测试

describe("Library", function() {

  beforeEach(function() {
  });

  it("should include correct parameters", function() {
      expect(params.param1).toEqual("");
      expect(params.param2).toEqual("link");
      expect(params.param3).toEqual("1");
      expect(params.param4).toEqual("1");
  });

  it("should show that method doSomething is called with zero arguments", function() {
          // I'm not sure how to write test for this.
  });
});
4

2 回答 2

16

我想你想使用toHaveBeenCalledWith()

it("should show that method doSomething is called with zero arguments", function() {
    // Ensure the spy was called with the correct number of arguments
    // In this case, no arguments
    expect(func.doSomething).toHaveBeenCalledWith();
});
于 2012-05-30T17:53:48.697 回答
2

如果 spy 函数只被调用过一次,请使用toHaveBeenCalledOnceWithmatcher:

expect(mySpy).toHaveBeenCalledOnceWith('', 'link', "1", "1");

它结合toHaveBeenCalledTimes(1)toHaveBeenCalledWith()匹配器。

Jasmine 3.6.

于 2020-11-03T20:26:35.433 回答