Jasmine object “has no method 'andReturn'”
10,339
Solution 1
jasmine 2.0 changed some of the spy syntax. jasmine 2.0 docs
spyOn(Math, 'random').and.returnValue(1);
Solution 2
try this
spyOn(Math, 'random').and.returnValue(1);
Solution 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!
Solution 4
use and.returnValue() insted of andReturn()
Related videos on Youtube
Author by
i_made_that
Updated on September 14, 2022Comments
-
i_made_that over 1 year
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!!!
-
i_made_that about 10 yearsThanks! I actually think that the problem might have something to do with the fact that I was using dist 2.0. I recreated the exact tests in 1.3.1 and they worked just fine.
-
i_made_that about 10 years
By chaining the spy with and.returnValue, all calls to the function will return a specific value.
-
i_made_that about 10 yearsYes! Looks like andReturn is now and.returnValue.
-
David Tengdin about 10 yearsThis is the current 2.0.0 jasmine syntax
-
Jezen Thomas almost 10 yearsWould have been better to provide the code in the answer, rather than just link to the docs.