Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
24
rated 0 times [  30] [ 6]  / answers: 1 / hits: 63032  / 10 Years ago, wed, june 18, 2014, 12:00:00

I'm testing router and have two functions, and I need to test if first function was called and second was not. There is method toHaveBeenCalled but there is no method to test if function was not called. How can I test that?



I have code like this:



var args, controller, router;
beforeEach(function() {
controller = {
foo: function(name, id) {
args = [].slice.call(arguments);
},
bar: function(name) {
}
};
spyOn(controller, foo).and.callThrough();
spyOn(controller, bar).and.callThrough();
router = new route();
router.match('/foo/bar/{{id}}--{{name}}', controller.foo);
router.match('/foo/baz/{{id}}--{{name}}', controller.bar);
router.exec('/foo/bar/10--hello');
});
it('foo route shuld be called', function() {
expect(controller.foo).toHaveBeenCalled();
});
it('bar route shoud not be called', function() {
// how to test if bar was not called?
});

More From » jasmine

 Answers
42

Use the not operator:


expect(controller.bar).not.toHaveBeenCalled();

[#70527] Monday, June 16, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
breonnamayah

Total Points: 574
Total Questions: 115
Total Answers: 96

Location: England
Member since Sun, May 21, 2023
1 Year ago
;