javascript - 如何测试该方法是否已在 Jasmine 中调用?

标签 javascript testing bdd jasmine

我对监视 Jasmine 有点困惑。我有这样的代码,但我不确定如何测试它。

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

最佳答案

我想你想使用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();
});

关于javascript - 如何测试该方法是否已在 Jasmine 中调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10817694/

相关文章:

java - 生成可验证的随机数 - Java

html - 如何使用 Cucumber 测试 HTML5 页面中的错误条件?

cucumber - 即使我的给定陈述是错误的,我的 cucumber 场景仍在继续

javascript - 使用参数从 firebase 查询数据 - React

javascript - 如何居中一个绝对位置的div?

asp.net - 在确认弹出窗口中取消来自 javascript 的回发

BDD 目标和属性

javascript - crm 2011 xrm.page.getattribute 有值时返回null

ruby-on-rails - 如何使用 RSpec 更新测试模型中的属性

java - Spring boot集成测试web配置