1

I am trying to write unit test cases using jasmine. I have a scenario where I am passing data to function with different properties I just want to check if that function is getting executed successfully or with out any error. I tried expect(foo).toHaveBeenCalled() but it expects a spy instead of a function. Please suggest any way to write a test case expectation in this scenario.

Sonali
  • 2,223
  • 6
  • 32
  • 69

2 Answers2

1

toHaveBeenCalled() can be used only with the spy.

Create a spy on your method and try this

spyOn(obj, 'foo');
obj.foo();  // call foo()
expect(obj.foo).toHaveBeenCalled()
Amit Chigadani
  • 28,482
  • 13
  • 80
  • 98
0

What are you trying to test, exactly?

  1. Test that foo is working correctly. In that case, just call foo() from your test. If it throws, the test will fail.

  2. Test that some other function bar is working correctly, where "correctly" means that it should call foo. In that case, create a spy for foo and use toHaveBeenCalled() on the spy. This may require that you restructure your code so you can somehow inject the foo spy into bar.

From your description, it seems that you want both at the same time, which goes against the principle of unit testing: you test one thing at a time.

Thomas
  • 174,939
  • 50
  • 355
  • 478