chai-as-promised

How to unit test a method which connects to mongo, without actually connecting to mongo?

时光毁灭记忆、已成空白 提交于 2019-12-03 01:23:29
问题 I'm trying to write a test to test a method that connects to mongo, but I don't actually want to have to have mongo running and actually make a connection to it to have my tests pass successfully. Here's my current test which is successful when my mongo daemon is running. describe('with a valid mongo string parameter', function() { it('should return a rejected promise', function(done) { var con = mongoFactory.getConnection('mongodb://localhost:27017'); expect(con).to.be.fulfilled; done(); });

How to unit test a method which connects to mongo, without actually connecting to mongo?

谁都会走 提交于 2019-12-02 14:45:21
I'm trying to write a test to test a method that connects to mongo, but I don't actually want to have to have mongo running and actually make a connection to it to have my tests pass successfully. Here's my current test which is successful when my mongo daemon is running. describe('with a valid mongo string parameter', function() { it('should return a rejected promise', function(done) { var con = mongoFactory.getConnection('mongodb://localhost:27017'); expect(con).to.be.fulfilled; done(); }); }); mongoFactory.getConnection code: getConnection: function getConnection(connectionString) { // do

chai-as-promised tests don't work with $q promises

喜欢而已 提交于 2019-11-30 08:35:59
问题 I'm trying to get chai-as-promised to work with $q promises with karma unit tests. svc.test = function(foo){ if (!foo){ // return Promise.reject(new Error('foo is required')); return $q.reject(new Error('foo is required')); } else { // get data via ajax here return $q.resolve({}); } }; it.only('should error on no foo', function(){ var resolvedValue = MyServices.test(); $rootScope.$apply(); return resolvedValue.should.eventually.be.rejectedWith(TypeError, 'foo is required'); }); The unit test

Do I really need to return a promise in test when using Chai as Promised?

时光毁灭记忆、已成空白 提交于 2019-11-29 12:09:43
Chai as Promised documentation states as follows: Notice : either return or notify(done) must be used with promise assertions. And the examples on the site are as follows: return doSomethingAsync().should.eventually.equal("foo"); doSomethingAsync().should.eventually.equal("foo").notify(done); The thing is; I actually wrote a test using chai as promised without returning the promise. Like so: it('should resolve user', function () { $state.get(state).resolve.user(dataservice, { userId: testUser.id }).should.eventually.eq(testUser); $rootScope.$apply(); }); And it works perfectly fine. I am sure

How to test promises with Mocha

北城余情 提交于 2019-11-26 20:40:38
问题 I'm using Mocha to test an asynchronous function that returns a promise. What's the best way to test that the promise resolves to the correct value? 回答1: Mocha has built-in Promise support as of version 1.18.0 (March 2014). You can return a promise from a test case, and Mocha will wait for it: it('does something asynchronous', function() { // note: no `done` argument return getSomePromise().then(function(value) { expect(value).to.equal('foo'); }); }); Don't forget the return keyword on the