javascript Jasmine 对象“没有方法‘andReturn’”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21590005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jasmine object “has no method 'andReturn'”
提问by i_made_that
Beginner with Jasmine, very first attempt with Jasmine Spies. I thought I was mimicking the format displayed here(search: "andReturn"), but I'm getting an error that I can't work out:
Jasmine 的初学者,Jasmine Spies 的第一次尝试。我以为我在模仿此处显示的格式(搜索:“andReturn”),但我收到了一个无法解决的错误:
TypeError: Object function () {
callTracker.track({
object: this,
args: Array.prototype.slice.apply(arguments)
});
return spyStrategy.exec.apply(this, arguments);
} has no method 'andReturn'
No clue what I'm doing wrong. Here's my Spec:
不知道我做错了什么。这是我的规格:
describe('Die', function() {
it('returns a value when you roll it', function() {
var die = Object.create(Die);
spyOn(Math, 'random').andReturn(1);
expect(die.roll()).toEqual(6);
});
});
And the corresponding JS:
以及相应的JS:
var Die =
{
roll: function() {
return Math.floor(Math.random() * 5 + 1);
}
}
Thanks for the help!!!
谢谢您的帮助!!!
回答by Gregg
jasmine 2.0 changed some of the spy syntax. jasmine 2.0 docs
jasmine 2.0 更改了一些 spy 语法。茉莉花 2.0 文档
spyOn(Math, 'random').and.returnValue(1);
回答by Terry
try this
试试这个
spyOn(Math, 'random').and.returnValue(1);
spyOn(Math, 'random').and.returnValue(1);
回答by hassassin
I made a jasmine test where I show this kind of mock. andReturn seems to be working. http://jsfiddle.net/LNWXn/
我做了一个茉莉花测试,我展示了这种模拟。andReturn 似乎有效。http://jsfiddle.net/LNWXn/
it("has a value of 1 with and return", function() {
spyOn(Math, 'random').andReturn(1);
expect(Math.random()).toBe(1);
});
You have to keep in mind that it's only mocked for the scope of the test. Here's one with your example that seems to pass. http://jsfiddle.net/LNWXn/2/
您必须记住,它只是针对测试范围进行模拟。这是您的示例中的一个似乎通过了。http://jsfiddle.net/LNWXn/2/
Hope this helped!
希望这有帮助!
回答by Ashish Gupta
use and.returnValue() insted of andReturn()
使用 and.returnValue() 插入 andReturn()