简体   繁体   English

如何测试是否在 jasmine 中调用了该方法?

[英]How do I test that the method has been called in jasmine?

I'm a bit confused with spying on jasmine.我对监视茉莉花有点困惑。 I have a code like this, but I'm not sure how to test it.我有一个这样的代码,但我不知道如何测试它。

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

How do test that func.doSomething has been called.如何测试 func.doSomething 已被调用。

This is the test I have written so far这是我迄今为止编写的测试

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

I think you want to use toHaveBeenCalledWith() :我想你想使用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();
});

If spy function has been called exactly once, use toHaveBeenCalledOnceWith matcher:如果 spy 函数只被调用过一次,使用toHaveBeenCalledOnceWith匹配器:

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

It combines toHaveBeenCalledTimes(1) and toHaveBeenCalledWith() matchers.它结合了toHaveBeenCalledTimes(1)toHaveBeenCalledWith()匹配器。

Coming with Jasmine 3.6 .Jasmine 3.6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript单元测试Jasmine /单元测试-如何测试是否已调用私有函数? - Javascript Unittesting Jasmine / Unit Testing - how do I test whether a private function has been called? 您需要间谍来测试是否已在Jasmine中调用函数吗? - Do you need spies to test if a function has been called in Jasmine? 如何测试方法是否只被调用一次而不是第二次调用Jasmine? - How to test if method has been called only once and not the second time in Jasmine? 茉莉花:如何测试GET请求中是否已调用正确的URL - Jasmine: How to test if the correct URL has been called in a GET request 使用Jasmine如何检查是否已调用本地方法? - Using Jasmine how to check if a local method has been called? Jasmine-成功从Ajax内部调用DOM元素的测试方法 - Jasmine - Test method has been called on DOM element from inside Ajax success 如何模拟茉莉花中其他服务方法中已调用的$ http.post方法? - How to mock $http.post method that has been called in other service method in jasmine? 如何验证使用内部测试框架调用过的方法? - How can I verify that a method has been called with intern test framework? 如何断言我的Jasmine测试中调用了一种方法 - How to assert that a method is called in my Jasmine test 如何测试一个函数没有被调用? - How can I test that a function has not been called?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM