spyOn:期待一个间谍,但得到了功能
spyOn: Expected a spy, but got Function
我正在使用 Jasmine 框架创建一些 Javascript 测试。我正在尝试使用 spyOn()
方法来确保已调用特定函数。这是我的代码
describe("Match a regular expression", function() {
var text = "sometext"; //not important text; irrelevant value
beforeEach(function () {
spyOn(text, "match");
IsNumber(text);
});
it("should verify that text.match have been called", function () {
expect(text.match).toHaveBeenCalled();
});
});
但我得到了
Expected a spy, but got Function
错误。我试图删除 spyOn(text, "match");
行,但它给出了同样的错误,似乎功能 spyOn()
不工作是应该的。
有什么想法吗?
您收到该错误是因为它在 expect
方法上失败。 expect
方法期望传入一个间谍,但实际上没有。要解决此问题,请执行以下操作:
var text = new String("sometext");
你的测试用例仍然会失败,因为你没有在任何地方调用 match 方法。如果你想让它通过,那么你需要在 it
函数中调用 text.match(/WHATEVER REGEX/)。
我发现为了测试像 string.match 或 string.replace 这样的东西,你不需要间谍,而是声明包含你正在匹配或替换的内容的文本并调用函数在 beforeEach 中,然后检查响应是否与您期望的相同。这是一个简单的例子:
describe('replacement', function(){
var text;
beforeEach(function(){
text = 'Some message with a newline \n or carriage return \r';
text.replace(/(?:\[rn])+/g, ' ');
text.replace(/\s\s+/g, ' ');
});
it('should replace instances of \n and \r with spaces', function(){
expect(text).toEqual('Some message with a newline or carriage return ');
});
});
这样就成功了。在这种情况下,我还会跟进一个替换以将多个间距减少到单个间距。此外,在这种情况下,beforeEach
不是必需的,因为您可以在 it
语句中并在您期望之前使用赋值和调用您的函数。它应该与 string.match
操作类似,如果你将它翻转过来阅读更像 expect(string.match(/someRegEx/).toBeGreaterThan(0);
.
希望这对您有所帮助。
-C§
编辑:或者,您可以将 str.replace(/regex/);
或 str.match(/regex/);
压缩到调用的函数中,然后在其中使用 spyOn
并在 spyOn(class, 'function').and.callthrough();
中使用 beforeEach
并使用 expect(class.function).toHaveBeenCalled();
和 var result = class.function(someString);
之类的东西(而不仅仅是调用函数)将允许您使用 expect(class.function(someString)).toEqual(modifiedString);
测试 return 值以进行替换或 expect(class.function(someString)).toBeGreaterThan(0);
匹配。
如果这提供了更深入的见解,请随时 +1。
谢谢,
C§
我正在使用 Jasmine 框架创建一些 Javascript 测试。我正在尝试使用 spyOn()
方法来确保已调用特定函数。这是我的代码
describe("Match a regular expression", function() {
var text = "sometext"; //not important text; irrelevant value
beforeEach(function () {
spyOn(text, "match");
IsNumber(text);
});
it("should verify that text.match have been called", function () {
expect(text.match).toHaveBeenCalled();
});
});
但我得到了
Expected a spy, but got Function
错误。我试图删除 spyOn(text, "match");
行,但它给出了同样的错误,似乎功能 spyOn()
不工作是应该的。
有什么想法吗?
您收到该错误是因为它在 expect
方法上失败。 expect
方法期望传入一个间谍,但实际上没有。要解决此问题,请执行以下操作:
var text = new String("sometext");
你的测试用例仍然会失败,因为你没有在任何地方调用 match 方法。如果你想让它通过,那么你需要在 it
函数中调用 text.match(/WHATEVER REGEX/)。
我发现为了测试像 string.match 或 string.replace 这样的东西,你不需要间谍,而是声明包含你正在匹配或替换的内容的文本并调用函数在 beforeEach 中,然后检查响应是否与您期望的相同。这是一个简单的例子:
describe('replacement', function(){
var text;
beforeEach(function(){
text = 'Some message with a newline \n or carriage return \r';
text.replace(/(?:\[rn])+/g, ' ');
text.replace(/\s\s+/g, ' ');
});
it('should replace instances of \n and \r with spaces', function(){
expect(text).toEqual('Some message with a newline or carriage return ');
});
});
这样就成功了。在这种情况下,我还会跟进一个替换以将多个间距减少到单个间距。此外,在这种情况下,beforeEach
不是必需的,因为您可以在 it
语句中并在您期望之前使用赋值和调用您的函数。它应该与 string.match
操作类似,如果你将它翻转过来阅读更像 expect(string.match(/someRegEx/).toBeGreaterThan(0);
.
希望这对您有所帮助。
-C§
编辑:或者,您可以将 str.replace(/regex/);
或 str.match(/regex/);
压缩到调用的函数中,然后在其中使用 spyOn
并在 spyOn(class, 'function').and.callthrough();
中使用 beforeEach
并使用 expect(class.function).toHaveBeenCalled();
和 var result = class.function(someString);
之类的东西(而不仅仅是调用函数)将允许您使用 expect(class.function(someString)).toEqual(modifiedString);
测试 return 值以进行替换或 expect(class.function(someString)).toBeGreaterThan(0);
匹配。
如果这提供了更深入的见解,请随时 +1。
谢谢,
C§