mocking defer object with jasmine


Often you come across deferred object, that looks like the following code.



When you're writing unit test, you need to mock out this component. It is also a standard practice to inject external components on your constructor and then we can mock it out.

This is how our mock quote service in typescript looks like.




We have retrieve, then and catch function. Closer analysis (Line  #8), you can actually see that we're returning some results from then function and it sets data in our main component

Line #7, result parameter in then function is a function which we can execute and sets data it needs. This allows us to fake some data -> we provide a fake quoteNumber,

With this, we don't really have to use spyOn and we get to set our data as tho it is the real implementation.

This is how it all ties together





Comments

Popular posts from this blog

ionic2 cordova build android - Unable resolve gradle 2.2.3

OpenCover code coverage for .Net Core

Using Custom DLL with IronPython / Scripts