Created
October 31, 2013 02:56
-
-
Save matiboy/7243743 to your computer and use it in GitHub Desktop.
Extending the idea of promise returning spy from http://decodify.blogspot.co.uk/2013/10/angularjs-unit-testing-helper-for.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var jasmineNG = {}; | |
| //set $q in your test | |
| jasmineNG.$q = null; | |
| //Could make similar to test a failing promise | |
| jasmineNG.createPromiseReturningSpy = function() { | |
| var deferred = jasmineNG.$q.defer(); | |
| // Use .andReturn to simply return a promise | |
| var spy = jasmine.createSpy().andReturn(deferred.promise); | |
| // Add resolve and reject functionalities | |
| spy.andResolveWith = function(val) { | |
| deferred.resolve(val); | |
| return spy; // chain | |
| }; | |
| spy.andRejectWith = function(val) { | |
| deferred.reject(val); | |
| return spy; | |
| }; | |
| return spy; | |
| } | |
| // Usage: | |
| // var spiedFunction = jasmineNG.createPromiseReturningSpy(); // always returns a non-resolved, non-rejected promise | |
| // var spiedFunctionResolved = jasmineNG.createPromiseReturningSpy().andResolveWith('hello'); // promise with resolve | |
| // var spiedFunctionResolved = jasmineNG.createPromiseReturningSpy().andRejectWith('no good'); // promise with reject |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment