Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I test that a function has not been called?

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? }); 
like image 560
jcubic Avatar asked Jun 18 '14 09:06

jcubic


People also ask

How do you check if a function has been called?

You can log a message when the function is called using: Debug. Log("Function called!"); You can store a bool that starts as false and set it to true when you enter the function. You can then check this bool elsewhere in code to tell whether your function has been called.

How do you test a function that does not return anything?

If your function is supposed to assert something and raise an error, give it wrong information and check if it does raise the right error. If your function takes an object and modifies it, test if the new state of your object is as expected.


1 Answers

Use the not operator:

expect(controller.bar).not.toHaveBeenCalled(); 
like image 112
griffon vulture Avatar answered Sep 22 '22 15:09

griffon vulture