sinon promise util

stable
Juanfran 2015-05-14 09:04:20 +02:00
parent 623b562192
commit f90cde8928
3 changed files with 40 additions and 12 deletions

View File

@ -60,19 +60,15 @@ describe "UserService", ->
{id: 3, name: "fake3"} {id: 3, name: "fake3"}
]) ])
mocks.resources.users.getContacts = (userId) -> mocks.resources.users.getContacts = sinon.stub().promise()
expect(userId).to.be.equal(userId) mocks.resources.users.getContacts.withArgs(userId).resolve(contacts)
return $q (resolve, reject) ->
resolve(contacts)
userService.attachUserContactsToProjects(userId, projects).then (_projects_) -> userService.attachUserContactsToProjects(userId, projects).then (_projects_) ->
contacts = _projects_.get(0).get("contacts") contacts = _projects_.get(0).get("contacts")
expect(contacts.get(0).get("name")).to.be.equal('fake1') expect(contacts.get(0).get("name")).to.be.equal('fake1')
done()
$rootScope.$apply() done()
it "get user contacts", (done) -> it "get user contacts", (done) ->
userId = 2 userId = 2
@ -83,11 +79,8 @@ describe "UserService", ->
{id: 3} {id: 3}
] ]
mocks.resources.users.getContacts = (userId) -> mocks.resources.users.getContacts = sinon.stub().promise()
expect(userId).to.be.equal(userId) mocks.resources.users.getContacts.withArgs(userId).resolve(contacts)
return $q (resolve, reject) ->
resolve(contacts)
userService.getContacts(userId).then (_contacts_) -> userService.getContacts(userId).then (_contacts_) ->
expect(_contacts_).to.be.eql(contacts) expect(_contacts_).to.be.eql(contacts)

View File

@ -18,6 +18,8 @@ module.exports = function(config) {
'karma.app.conf.js', 'karma.app.conf.js',
'dist/js/libs.js', 'dist/js/libs.js',
'node_modules/angular-mocks/angular-mocks.js', 'node_modules/angular-mocks/angular-mocks.js',
'node_modules/bluebird/js/browser/bluebird.js',
'test-utils.js',
'dist/js/app.js', 'dist/js/app.js',
'dist/js/templates.js', 'dist/js/templates.js',
'app/**/*spec.coffee' 'app/**/*spec.coffee'

33
test-utils.js Normal file
View File

@ -0,0 +1,33 @@
(function() {
var searchOriginal = function(obj) {
if (obj._promise) {
return obj;
} else {
return searchOriginal(obj.parent);
}
};
sinon.stub.promise = function() {
var obj = this;
var returnedPromise = new Promise(function(_resolve_, _reject_){
obj._resolvefn = _resolve_;
obj._rejectfn = _reject_;
});
this.returns(returnedPromise);
this._promise = true;
return this;
};
sinon.stub.resolve = function() {
var original = searchOriginal(this);
original._resolvefn.apply(this, arguments);
};
sinon.stub.reject = function() {
var original = searchOriginal(this);
original._rejectfn.apply(this, arguments);
};
}());