Jasmine 的间谍 toHaveBeenCalled 方法存在问题

发布于 2025-01-08 15:27:27 字数 648 浏览 3 评论 0原文

谁能告诉我为什么以下测试失败。

var Person = function() {};

Person.prototype.helloSomeone = function(toGreet) {
  return this.sayHello() + " " + toGreet;
};

Person.prototype.sayHello = function() {
  return "Hello";
};

describe("Person", function() {
 it("calls the sayHello() function", function() {
   var fakePerson = new Person();
   spyOn(fakePerson, "sayHello");
   fakePerson.helloSomeone("world");
   expect(fakePerson.sayHello).toHaveBeenCalled();
  });
});

我从此处获取它,他说它有效。我可以看到spyOn方法正在person对象上创建一个同名的包装函数,即fakePerson.sayHello正在对象上调用,而不是原型上。

非常感谢

Can anyone possibly tell me why the following test fails.

var Person = function() {};

Person.prototype.helloSomeone = function(toGreet) {
  return this.sayHello() + " " + toGreet;
};

Person.prototype.sayHello = function() {
  return "Hello";
};

describe("Person", function() {
 it("calls the sayHello() function", function() {
   var fakePerson = new Person();
   spyOn(fakePerson, "sayHello");
   fakePerson.helloSomeone("world");
   expect(fakePerson.sayHello).toHaveBeenCalled();
  });
});

I took it from here and he said it works. I can see the spyOn method is creating a wrapper function of the same name on the person object i.e. fakePerson.sayHello is being invoked on the object and not the prototype.

Many thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

離殇 2025-01-15 15:27:27

测试失败了,因为我也在使用 jasmine-sinon.js。

The tests failed because I was also using jasmine-sinon.js.

窗影残 2025-01-15 15:27:27

您的测试用例的一个可能的问题是您没有指定应该调用原始方法。正确的行为如下(注意“andCallThrough”):

describe("Person", function() {
 it("calls the sayHello() function", function() {
   var fakePerson = new Person();
   spyOn(fakePerson, "sayHello").andCallThrough();
   fakePerson.helloSomeone("world");
   expect(fakePerson.sayHello).toHaveBeenCalled();
  });
});

您可以查看 Jasmine 的文档页面,了解有关其他可能性的更多信息:https://github.com/pivotal/jasmine/wiki/Spies

编辑:快速浏览一下 jasmine-sinon 文档显示以下内容:

<块引用>

警告

jasmine-sinon 当前会覆盖用于其自身间谍功能的同名 Jasmine 匹配器。我计划允许将来选择性地保留这些。

被覆盖的本机 Jasmine 匹配器是:

  • toHaveBeenCalled()
  • toHaveBeenCalledWith()

如果你想使用 jasmine-sinon,你必须使用他们的 API,而不是 Jasmine 的 API。

编辑:自2012年2月起:

您还可以将茉莉花间谍与诗浓间谍一起使用。
jasmine-sinon 将检测您正在使用哪个并使用适当的匹配器。

One possible problem with your test cases is that you haven't specified that the original method should be called. What would have a proper behavior is the following (note the "andCallThrough") :

describe("Person", function() {
 it("calls the sayHello() function", function() {
   var fakePerson = new Person();
   spyOn(fakePerson, "sayHello").andCallThrough();
   fakePerson.helloSomeone("world");
   expect(fakePerson.sayHello).toHaveBeenCalled();
  });
});

You can see the documentation page of Jasmine for more information about the other possibility : https://github.com/pivotal/jasmine/wiki/Spies

Edit: A quick look at the jasmine-sinon documentation brings up the following:

Warning

jasmine-sinon currently overwrites any Jasmine matchers of the same name used for its own spying features. I plan to allow these to be optionally retained in the future.

The native Jasmine matchers that are overwritten are:

  • toHaveBeenCalled()
  • toHaveBeenCalledWith()

If you want to use jasmine-sinon, you have to use their API and not the one of Jasmine.

Edit: As of Feb 2012:

You can also use Jasmine spies alongside your Sinon spies.
jasmine-sinon will detect which you're using and use the appropriate matcher.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文