-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(counterfeit): add api for creating promises and stubs
A factory called `counterfeit` has been added. This factory facilitates the testing of methods that use asynchronous promises, by providing an api to generate fake promises and stub functions that return a promise. An integration test has been added for the `DeathStar` test fixture. Within this test the `DeflectorShield` class is decorated so that the `reboot` method is a `CounterfeitStub`. This stub is configured to return a `CounterfeitPromise`, which can be conveniently resolved within the test. Behind the scenes `CounterfeitPromise` are built using the AngularJs `$q` service.
- Loading branch information
Showing
5 changed files
with
101 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
counterfeit.factory("counterfeit", function(CounterfeitPromise, CounterfeitStub) { | ||
return { | ||
promise: function () { | ||
return CounterfeitPromise.create(); | ||
}, | ||
|
||
stub: function(promise) { | ||
return CounterfeitStub.create(promise); | ||
} | ||
} | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
counterfeit.factory("CounterfeitPromise", function() { | ||
var $timeout, $q; | ||
|
||
function CounterfeitPromise() { | ||
this._injectDependecies(); | ||
this.reset(); | ||
} | ||
|
||
// Public | ||
// ------------------------------ | ||
CounterfeitPromise.prototype.resolve = function(value) { | ||
this._complete(this.deferred.resolve, value); | ||
}; | ||
|
||
CounterfeitPromise.prototype.reset = function() { | ||
this.deferred = $q.defer(); | ||
return this.deferred.promise; | ||
}; | ||
// ------------------------------ | ||
|
||
CounterfeitPromise.prototype._injectDependecies = function() { | ||
inject(function(_$q_, _$timeout_) { | ||
$q = _$q_; | ||
$timeout = _$timeout_; | ||
}); | ||
}; | ||
|
||
CounterfeitPromise.prototype._complete = function(fn, value) { | ||
$timeout(function(){fn(value);}); | ||
$timeout.flush(); | ||
}; | ||
|
||
return { | ||
create: function () { | ||
return new CounterfeitPromise(); | ||
} | ||
} | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
counterfeit.service("CounterfeitStub", function() { | ||
return { | ||
create: function(promise) { | ||
var p = promise.reset(); | ||
|
||
return function() { | ||
return { | ||
$promise: p | ||
} | ||
}; | ||
} | ||
}; | ||
}); |
This file contains 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