单元测试新手,尤其是Jasmine。使用Jasmine进行单元测试:beforeEach()中的代码未在测试的spyOn中看到()
我在beforeEach()
回调中设置了一个变量,但它在第二个测试中似乎不起作用。它应该在之前每隔进行初始化测试,对吧?我确定我的电话是spyOn()
,但我不知道如何解决。
评论解释通行证和失败:
describe("Test suite for my library", function() {
var html,
body,
play,
...
// custom matcher...
beforeEach(function() {
this.addMatchers({
toBeInstanceOf : function (constructr) {
return this.actual instanceof constructr;
});
});
});
describe("Within the Button object", function() {
beforeEach(function() {
play = new Button("play", false);
});
describe("play", function() {
// This test passes, as expected...
it("should be an instance of the Button object", function() {
expect(play).toBeInstanceOf(Button);
});
});
describe("play.name", function() {
// This test failed with the message
// "Expected spy Button to have been called
// with [ 'play', false ] but it was never called."
it("should be the first argument passed to the Button constructor", function() {
spyOn(window, "Button");
play = new Button("play", false); // ...until I added this line. Now it passes.
expect(window.Button).toHaveBeenCalledWith("play", false);
});
// This test passes, even if the one above fails.
it("should be 'play'", function() {
expect(play.name).toBe("play");
});
});
});
});
的documentation解释用法,而不是背景下,spyOn()
,所以我不能,如果我已经创建了一个错误或者说,如果我不知不觉中利用了一个功能。
如果有人认为它在诊断中有任何区别,我可以发布构造函数,但我可以向你保证它已经很简单。
我敢肯定,这是一个简单的修复,使用一些基本的单元测试概念,我不得不努力学习。提前致谢。
P.S.我意识到我正在测试的失败规范不是我所描述的。我正在通过API指南工作,寻找一种方法来获取函数调用中的参数数组,因此我可以在arguments[0]
上进行特定的测试。提示是赞赏,但不是必要的。我会弄清楚。
切换顺序和CallThrough()做到了。 +1并被接受。非常感谢。 – parisminton 2012-01-21 01:39:46
@parisminton。我在jamsine测试用例上发布了一个问题。 http://stackoverflow.com/questions/26583283/jasmine-junit-testing-of-delegate-callback-of-function-args ..真正appriciate如果你能帮助。问候 – 2014-10-28 13:58:18