When using mocha to test a model of a sails app, It doesn't seem to run tests inside a callback:
var assert = require('assert');
describe('Dataset', function() {
describe('create', function() {
it('should create a new dataset', function() {
Dataset.create({
'name': 'testDataSet',
'description': 'This dataset exists for testing purposes only.',
'visibility': 'private',
'data': {
"foo": {
"barn": "door",
"color": "green"
}
}
}, function(err, dataset) {
assert(false);
});
});
});
});
This test is called by a script that initializes sails for it.
when Running this test, it passes, even though it should fail.
$: mocha
1 passing (875ms)
Also it never seems to run the callback containing assert(false). Placing assert false at any other location yields the exepcted results, also sails seems to be running properly.
Any help is greatly appreciated.
Your code is asynchronous, so you should use callback in your testing function:
var assert = require('assert');
describe('Dataset', function() {
describe('create', function() {
it('should create a new dataset', function(done) {
Dataset.create({
'name': 'testDataSet',
'description': 'This dataset exists for testing purposes only.',
'visibility': 'private',
'data': {
"foo": {
"barn": "door",
"color": "green"
}
}
}, function(err, dataset) {
if (err) throw err;
done();
});
});
});
});
Add assert call wherever you need.
There are a lot of examples here.
Related
I have tried to insert the value in db in my mocha test i am getting this error i tried few of the following ways but nothing work out.
var assert=require('chai').assert;
const user=require('../model/user')
i tried both way
describe('insertDataLasone',()=>{
it('should save the value ',(done)=>{
var User = new user({fname:'test'});
User.save().then(done=>{
done()
}).catch(done=>done())
})
})
describe('User', function() {
describe('#save()', function() {
// this.timeout(5000)
it('should save without error', function(done) {
var User5 = new user({fname:'test'});
User5.save(function(done) {
if (err) done(err);
else setTimeout(done,3000);
});
});
});
});
This error occurs when done() is not called in a test. Make sure you are calling done().
var assert = require('chai').assert;
const User = require('../model/user');
describe('insertDataLasone', () => {
it('should save the value ', done => {
var user = new User({ fname: 'test' });
user.save().then(() => {
done();
})
.catch(done); // mocha done accepts Error instance
});
});
or
var assert = require('chai').assert;
const User = require('../model/user');
describe('User', function() {
describe('#save()', function() {
it('should save without error', function(done) {
var user5 = new User({ fname: 'test' });
user5.save(function(err) {
if (err) done(err);
else done();
});
});
});
});
Read https://mochajs.org/#asynchronous-code carefully
I followed the WebStorm video on how to setup Mocha in WebStorm:
https://www.youtube.com/watch?time_continue=81&v=4mKiGkokyx8
I created a very simple test with a pass and a fail:
var assert = require("assert")
describe('Array', function() {
describe('#indexOf()', function() {
it('should return -'), function() {
assert.equal(-1, [1,2,3].indexOf(5))
}
it('should fail'), function() {
assert.equal(1, [1,2,3].indexOf(5))
}
})
})
I then setup a run configuration like this:
And then I run it. It just states that the tests are 'pending' and then the process completes:
Why is this happening?
Your both tests are ignored, because you are using incorrect it() syntax. Please try changing your suite as follows:
var assert = require("assert")
describe('Array', function() {
describe('#indexOf()', function() {
it('should return -', function() {
assert.equal(-1, [1,2,3].indexOf(5))
})
it('should fail', function() {
assert.equal(1, [1,2,3].indexOf(5))
})
})
})
I'm using jasmine+karma to run the following code...
and get the following error:
Expected { then : Function, catch : Function, finally : Function } to equal 123.
Can someone help me understand why I don't get a resolved value for my promise. thanks
'use strict';
angular
.module('example', ['ui.router'])
.config(function($stateProvider) {
$stateProvider
.state('stateOne', {
url: '/stateOne',
resolve: {cb: function($q) {
var deferred = $q.defer();
deferred.resolve(123);
return deferred.promise;
}},
controller: function($scope, cb) {console.log(' * in controller', cb);},
templateUrl: 'stateOne.html'
});
})
.run(function($templateCache) {
$templateCache.put('stateOne.html', 'This is the content of the template');
});
describe('main tests', function() {
beforeEach(function() {module('example');});
describe('basic test', function($rootScope) {
it('stateOne', inject(function($rootScope, $state, $injector, $compile) {
var config = $state.get('stateOne');
expect(config.url).toEqual('/stateOne');
$compile('<div ui-view/>')($rootScope);
$rootScope.$digest();
expect($injector.invoke(config.resolve.cb)).toEqual(123);
}));
});
});
Ok, Figured it out with some help (via email) from Nikas, whose blog I found at:
http://nikas.praninskas.com/angular/2014/09/27/unit-testing-ui-router-configuration/.
Here is a succinct example that demonstrates how to test the resolve values in ui.router, where the values involve $http.
angular
.module('example', ['ui.router'])
.factory('Clipboard', function($http) {
return {
get: function(args) {
return $http.get('/db/clipboard');
}
};
})
.config(function($stateProvider) {
$stateProvider
.state('stateOne', {
resolve: {cb: function(Clipboard) {
return Clipboard.get();
}}
});
});
describe('main tests', function() {
beforeEach(function() {module('example');});
it('stateOne', inject(function($state, $injector, $httpBackend) {
$httpBackend.whenGET('/db/clipboard').respond({a:1});
$injector.invoke($state.get('stateOne').resolve['cb'])
.then(function(res) {console.log(' *res ', res.data);})
.catch(function(err) {console.log(' *err ', err);});
$httpBackend.flush();
}));
afterEach(inject(function($httpBackend) {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
}));
});
Mocha multiple test in order
I am writting test with mocha like this
describe('application', function(){
describe('First set of test', function(){
var socket;
before(function(done) {
var opts = {
'reconnection delay' : 0,
'reopen delay' : 0,
'forceNew' : true
};
socketio = io.connect(SOCKET_URL, opts);
done();
socketio.on('connect', function() {
console.log('worked...');
});
socketio.on('disconnect', function() {
console.log('disconnected...');
})
});
it('first test should be true', function(done){
expect(variable_one).to.be.false;
done();
});
it('second test should be true', function(done){
client.hget(hash, "status", function(err, result){
expect(variable_two).to.be.true;
done();
})
});
it('thrid test should be true', function(done){
client.hget(hash, "status", function(err, result){
expect(variable_three).to.be.true;
done();
})
});
});
});
What I need is that test should be nested in order, to set the before variables is not enough because the third test depends on the second and the second on the first one. With the done variable are all running in parallel.
Is there a posible way to achieve this?
Thank you
I have two function:
test.set(object details)
test.onChanged.addListener(function(object info) {...})
fired onChanged when a test is set.
So, how should I use qunit and promise to test it?
QUnit.asyncTest("onChanged with set", function(assert) {
new Promise(function(r1) {
test.set({});
r1();
}).then(function() {
new Promise(function(r2) {
test.onChanged.addListener(function(info) {
assert.ok(true, "onChanged callback");
assert.deepEqual(info, {}, "onChanged result");
r2();
});
});
}).then(function() {
QUnit.start();
});
});
But, it does not run.