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!!!
jasmine 2.0 changed some of the spy syntax. jasmine 2.0 docs
spyOn(Math, 'random').and.returnValue(1);
try this
spyOn(Math, 'random').and.returnValue(1);
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!
use and.returnValue() insted of andReturn()
Related
Im going to create a custom matcher in Jasmine 2.0 to verify spies against some additional conditions. In huge simplification, something like:
var customMatchers = {
toDoSomething: function(util, customEqualityTesters) {
return {
compare: function(spy) {
var comparison = {};
comparison.pass = testSomeCondition(spy);
if (!comparison.pass) {
comparison.message = "Expect " + /insert code here/ + " to do something";
}
return comparison;
}
}
}
};
beforeEach(function() {
jasmine.addMatchers(customMatchers);
});
My question is, how to recover the spy name, passed as a first argument of factory method: createSpy(name, originalFn)?
I cannot find anything in Jasmine documentation v2.6 neither in online tutorials.
console.log(spy) returns function(...) {...}
I don't know if it is the right thing to do so, but found in Jasmine source code how toHaveBeenCalled was originally implemented and found:
spy.and.identity()
It works well also in a custom matcher.
I have an Angular 1.6.6 application which I'm testing with Karma and Jasmine.
Given this code from controller:
$scope.undo = function () {
return $scope.isUndoDisabled() || $scope.undoAction();
};
$scope.isUndoDisabled = function () {
return HistoryService.isUndoDisabled();
};
I have been testing it with the following spec:
it('undoAction should not be called with default settings', function () {
var $scope = {};
var controller = $controller('PaintController', { $scope: $scope });
spyOn($scope, 'undoAction');
//spyOn(HistoryService, 'isUndoDisabled');
$scope.undo();
expect($scope.undoAction).not.toHaveBeenCalled();
});
And is passing the test, but when I uncomment the spyOn of HistoryService, the call HistoryService.isUndoDisabled() from $scope.isUndoDisabled returns undefined and then the test fails because:
Expected spy undoAction not to have been called.
Any idea about what's going on???? It seems like the spyOn is affecting to the code??
spyOn(...) is a shortcut for spyOn(...).and.stub(), not spyOn(...).and.callThrough(). When being spied this way, HistoryService.isUndoDisabled() returns undefined.
The proper way to test the unit is to isolate it from others. Since it is the controller that is tested, the service should be mocked or stubbed:
spyOn(HistoryService, 'isUndoDisabled').and.returnValue(true);
And then in another test:
spyOn(HistoryService, 'isUndoDisabled').and.returnValue(false);
I think if you want to call isUndoDisabled() from HistoryService, the function $scope.isUndoDisabled should be
$scope.isUndoDisabled = function () {
HistoryService.isUndoDisabled();
};
There shouldn't be a return in the body
I have run into a problem when writing unit tests in jasmine which I have managed to distill down to a a very basic test scenario:
describe('weird shit', function () {
var myVal;
beforeEach(myVal = 0);
it('throws for some reason', function () {
expect(myVal).toBe(0);
});
});
and this throws a TypeError:
Test 'weird shit:throws for some reason' failed
TypeError: undefined is not a constructor (evaluating 'queueableFn.fn.call(self.userContext)') in
file:///C:/USERS/9200378/APPDATA/LOCAL/MICROSOFT/VISUALSTUDIO/14.0/EXTENSIONS/SKJV5WFA.151/TestFiles/jasmine/v2/jasmine.js (line 1886)
run#file:///C:/USERS/9200378/APPDATA/LOCAL/MICROSOFT/VISUALSTUDIO/14.0/EXTENSIONS/SKJV5WFA.151/TestFiles/jasmine/v2/jasmine.js:1874:20
execute#file:///C:/USERS/9200378/APPDATA/LOCAL/MICROSOFT/VISUALSTUDIO/14.0/EXTENSIONS/SKJV5WFA.151/TestFiles/jasmine/v2/jasmine.js:1859:13
queueRunnerFactory#file:///C:/USERS/9200378/APPDATA/LOCAL/MICROSOFT/VISUALSTUDIO/14.0/EXTENSIONS/SKJV5WFA.151/TestFiles/jasmine/v2/jasmine.js:697:42
execute#file:///C:/USERS/9200378/APPDATA/LOCAL/MICROSOFT/VISUALSTUDIO/14.0/EXTENSIONS/SKJV5WFA.151/TestFiles/jasmine/v2/jasmine.js:359:28
fn#file:///C:/USERS/9200378/APPDATA/LOCAL/MICROSOFT/VISUALSTUDIO/14.0/EXTENSIONS/SKJV5WFA.151/TestFiles/jasmine/v2/jasmine.js:2479:44
if I removed the beforeEach then it works fine:
describe('weird shit', function () {
var myVal =0;
it('throws for some reason', function () {
expect(myVal).toBe(0);
});
});
Which I do not understand, the beforeEach is very basic, please help.
beforeEach accepts a function, you passed the result of myVal = 0, which is 0, which isn't a function.
beforeEach(myVal = 0);
Replace the above code with the following:
beforeEach(function() {
myVal = 0;
});
Refer the documentation of jasmine 2.5 here for more information.
beforeEach(function(){myVal = 0;});
Purpose of beforeEach() is to execute some function that contains code to setup your specs.
In this case, it is setting variable myVal = 0
You should pass a function to beforeEach() like below:
beforeEach(function() {
myVal = 0
});
in order to successfully setup your variable.
I am trying to test .calls.count() and .calls.any, I tried the below code from this link Jasmine test cases
describe("A spy", function() {
var foo, bar = null;
beforeEach(function() {
foo = {
setBar: function(value) {
bar = value;
}
};
spyOn(foo, 'setBar');
});
it("tracks if it was called at all", function() {
expect(foo.setBar.calls.any()).toEqual(false);
foo.setBar();
expect(foo.setBar.calls.any()).toEqual(true);
});
it("tracks the number of times it was called", function() {
expect(foo.setBar.calls.count()).toEqual(0);
foo.setBar();
foo.setBar();
expect(foo.setBar.calls.count()).toEqual(2);
});
});
But this throws error saying:
TypeError: 'foo.setBar.calls.any' is not a function
and
TypeError: 'foo.setBar.calls.count' is not a function
I checked the syntax, it's same everywhere on the net. What's wrong?
Kind of a silly question, but are you sure you're not accidentally using Jasmine 1.3? calls was a valid property there too, but it didn't yet have the any() and count() methods, which you're referencing in the 2.0 docs.
(I've faced a lot of similar mistakes while migrating from 1.3 to 2.0, because the syntax is very similar, so the mistakes don't stand out at a glance.)
I had a similar problem, and then looked up this. The answer above is right, and I managed to fix it by explicitly including a reference to "jasmine2" in my testem.json, instead of just "jasmine":
{
"framework": "jasmine2",
"src_files": [
"filePath1.js",
"filePath2.js",
"filePathEtc.js"
]
}
I mactually trying to run my first unit test with mocha using this code :
var assert = require('assert');
var returnCool = function () {
return 1;
}
describe("Array contains", function () {
it('should return-1 when the value is not present', function () {
returnCool().should.equal(1);
});
});
The problem is that my code is actually failing everytime.
I tried with the sample in mocha website :
describe('Array', function(){
describe('#indexOf()', function(){
it('should return -1 when the value is not present', function(){
[1,2,3].indexOf(5).should.equal(-1);
[1,2,3].indexOf(0).should.equal(-1);
})
})
})
And it fails too.
What am I doing wrong ?
Thanks for advance
Looks like you are not calling your assertion library. You are currently calling .should() on an integer
You have included an assert library but are using should - style asserts. Either include should.js or use assert-style asserts (assert.equal([1,2,3].indexOf(5), -1))