diff --git a/app/modules/services/user.service.spec.coffee b/app/modules/services/user.service.spec.coffee index c1ef0cdb..d48b0fe5 100644 --- a/app/modules/services/user.service.spec.coffee +++ b/app/modules/services/user.service.spec.coffee @@ -60,19 +60,15 @@ describe "UserService", -> {id: 3, name: "fake3"} ]) - mocks.resources.users.getContacts = (userId) -> - expect(userId).to.be.equal(userId) - - return $q (resolve, reject) -> - resolve(contacts) + mocks.resources.users.getContacts = sinon.stub().promise() + mocks.resources.users.getContacts.withArgs(userId).resolve(contacts) userService.attachUserContactsToProjects(userId, projects).then (_projects_) -> contacts = _projects_.get(0).get("contacts") expect(contacts.get(0).get("name")).to.be.equal('fake1') - done() - $rootScope.$apply() + done() it "get user contacts", (done) -> userId = 2 @@ -83,11 +79,8 @@ describe "UserService", -> {id: 3} ] - mocks.resources.users.getContacts = (userId) -> - expect(userId).to.be.equal(userId) - - return $q (resolve, reject) -> - resolve(contacts) + mocks.resources.users.getContacts = sinon.stub().promise() + mocks.resources.users.getContacts.withArgs(userId).resolve(contacts) userService.getContacts(userId).then (_contacts_) -> expect(_contacts_).to.be.eql(contacts) diff --git a/karma.conf.js b/karma.conf.js index bc96834e..ae7b045e 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -18,6 +18,8 @@ module.exports = function(config) { 'karma.app.conf.js', 'dist/js/libs.js', 'node_modules/angular-mocks/angular-mocks.js', + 'node_modules/bluebird/js/browser/bluebird.js', + 'test-utils.js', 'dist/js/app.js', 'dist/js/templates.js', 'app/**/*spec.coffee' diff --git a/test-utils.js b/test-utils.js new file mode 100644 index 00000000..4b33b6ad --- /dev/null +++ b/test-utils.js @@ -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); + }; +}());