问题
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:
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:
var Die =
{
roll: function() {
return Math.floor(Math.random() * 5 + 1);
}
}
Thanks for the help!!!
回答1:
jasmine 2.0 changed some of the spy syntax. jasmine 2.0 docs
spyOn(Math, 'random').and.returnValue(1);
回答2:
try this
spyOn(Math, 'random').and.returnValue(1);
回答3:
I made a jasmine test where I show this kind of mock. andReturn seems to be working. 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/
Hope this helped!
回答4:
use and.returnValue() insted of andReturn()
来源:https://stackoverflow.com/questions/21590005/jasmine-object-has-no-method-andreturn