Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to spyOn a static class method with Jasmine

Tags:

I have a class with a static method that I want to test in Jasmine. I understand that static methods are not callable on instances of the class. So besides the fact that it cannot find the method to spyOn, my test does not pass, but how would one go about testing static methods in a class with Jasmine?

class Foo {     static foobar (a, b) {       return a * b     } } 

Jasmine Test

it ('should test a static method', () => {     let foo = new Foo()     spyOn(foo, 'foobar')     foo.foobar(2,3)     expect(foo.foobar).toBe(6) }) 
like image 970
Sherman Hui Avatar asked Jan 31 '17 01:01

Sherman Hui


1 Answers

You should be able to use spyOn(Foo, 'foobar') to make it a spy.

Also spies are not intended to directly be tested - they are a tool so that you can test other code more deterministically and in isolation.

like image 180
Daniel A. White Avatar answered Sep 21 '22 03:09

Daniel A. White