Cheat Sheet - Jasmine
We reuse many things during any application development. While working with Jasmine, generally there two sources, Jasmine Docs or Google. Even though we refer these resources, it consumes lot of time to get what we want i.e. right syntax and example.
To avoid doing this every time I have enlisted few of them as cheat sheet which can be used whenever required.
To avoid doing this every time I have enlisted few of them as cheat sheet which can be used whenever required.
- Create spy object with spy methods
mySpy = jasmine.createSpyObj('Contact', ['get','set']); - Call fake method instead of real one.
mySpy.pickContact.and.callFake(function () { return { id: 10 }; }); - Call a spy method and return a value
var contact = { id: 10 }; mySpy.pickContact.and.returnValue(contact); - Call a spy method and return a async value
var contact = { id: 10 }; mySpy.pickContact.and.returnValue(asyncData(contact)); - Check call counts
expect(mySpy.get.calls.count()).toBe(1, 'one call'); - Check most recent call
expect(mySpy.get.calls.mostRecent().args[1]).toEqual('getErr'); - Prefer spies as they are usually the easiest way to mock services.
- fakeAsync() with tick() → No actual call from spec file
- async() with fixture.whenStable() → Actual call from spec file
Comments
Post a Comment