I'm trying to setting up automated test.
I'm using zombiejs for the browser, mochaJs for the test framework and chaiJs for assertion. I want to use chai-jq for jquery assertion:
var expect = require('chai').expect
var chai = require("chai");
var chaiJq = require('chai-jq');
Browser = require('zombie'),
browser = new Browser();
chai.use(chaiJq);
describe('Test', function(){
before(function(done) {
browser.visit('http://localhost/login.php', done);
});
describe('Connexion au site', function() {
before(function(done) {
browser
.fill('login', 'foo')
.fill('password', 'bar')
.pressButton('Connexion', done);
});
it('should be successful (code 200)', function() {
browser.assert.success(200);
});
});
describe('', function() {
browser.visit('http://localhost/activites/nationales/accueil.php');
it('contain text', function() {
var $elem = $("<div id=\"hi\" foo=\"bar time\" />");
expect($elem)
// Assertion object is `$elem`
.to.have.$attr("id", "hi").and
// Assertion object is still `$elem`
.to.contain.$attr("foo", "bar");
});
});
When I run the test i've got the error ReferenceError: $ is not defined
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 am trying to test my Angular with Jasmine but somehow i keep getting this error Expected undefined to be defined and i have followed that angular documentation example i am on mean stack
test.js
describe('Testing Ecdreport Controllers', function(){
var $scope, controller;
var app = angular.module('mean.ecdreport',[])
.controller('EcdreportController', ['$scope', '$http', 'Global', 'Ecdreport', function($scope, $http, Global, Ecdreport) {
$scope.global = Global;
$scope.query = "";
$scope.package = {
name: 'ecdreport'
};
$scope.startDate = null;
$scope.endDate = null;
$scope.currentPage = 1;
$scope.child= [];
$scope.maxSize = 5;
$scope.items = [];
$scope.itemsPerPage = 10;
$scope.totalItems = null;
$scope.direction = 1;
$scope.directionOld = 1;
$scope.sortAttributes = 0;
$scope.sortAttributesOld = 0;
$scope.datamodel = null;
$scope.getDataModel = function() {
$http({url:'/api/v1/getdatamodel', method:"GET"})
.success(function(data) {
console.log('Datamodel successful');
$scope.datamodel = data[0];
console.log('datamodel', data);
})
.error(function(error) {
$scope.datamodel =[];
});
}
// console.log("Trying to get datamodel");
$scope.getDataModel();
});
describe('Testing Ecdreport Controllers', function(){
var $scope, controller;
beforeEach(module('mean.ecdreport', function($controllerProvider){
$controllerProvider.register('EcdreportController', function(){
});
}));
beforeEach(inject(function(_$rootScope_,_$controller_){
$scope = _$rootScope_.$new();
controller = _$controller_('EcdreportController',
{$scope : $scope
});
}));
it('Should be registered', function(){
expect(controller).toBeDefined();
})
it('Testing Scope', function(){
expect($scope).toBeDefined()
expect($Scope.getDataModel).toBeDefined();
})
});
beforeEach(module('mean.ecdreport', function($controllerProvider){
$controllerProvider.register('EcdreportController', function(){
});
}));
beforeEach(inject(function(_$rootScope_,_$controller_){
$scope = _$rootScope_.$new();
controller = _$controller_('EcdreportController',
{$scope : $scope
});
}));
it('Should be registered', function(){
expect(controller).toBeDefined();
})
it('Testing Scope', function(){
expect($scope).toBeDefined()
expect($scope.getDataModel).toBeDefined();
})
});
You get that error because your controller in test is never defined. You need to use var controller = ...
You should use controller injection like this :
beforeEach(inject(function(_$rootScope_,_$controller_){
$scope = _$rootScope_.$new();
createController = function() {
return _$controller_('EcdreportController', {
$scope : $scope
});
};
}));
and initialize the controller in each test like this :
it('Should be registered', function(){
var controller = new createController();
expect(controller).toBeDefined();
})
This way you can also pass on different parameters in each test if your controller requires any data to be passed on to.
I'm having difficulty testing that Reflux actions are triggering correctly in my application, and in fact they do not seem to be working at all with Jest. I have this example test:
jest.autoMockOff();
describe('Test', function () {
it('Tests actions', function () {
var Reflux = require('../node_modules/reflux/index');
var action = Reflux.createAction('action');
var mockFn = jest.genMockFn();
var store = Reflux.createStore({
init: function () {
this.listenTo(action, this.onAction);
},
onAction: function () {
mockFn();
}
});
action('Hello World');
expect(mockFn).toBeCalled();
});
});
Which outputs:
● Test › it Tests actions
- Expected Function to be called.
at Spec.<anonymous> (__tests__/Test.js:20:20)
at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)
Even with Jasmine async functions it doesn't seem to be working
jest.autoMockOff();
describe('Test', function () {
it('Tests actions', function () {
var Reflux = require('../node_modules/reflux/index');
var action = Reflux.createAction('action');
var mockFn = jest.genMockFn();
var flag = false;
var store = Reflux.createStore({
init: function () {
this.listenTo(action, this.onAction);
},
onAction: function () {
mockFn();
flag = true;
}
});
runs(function () {
action();
});
waitsFor(function () {
return flag;
}, 'The action should be triggered.', 5000);
runs(function () {
expect(mockFn).toBeCalled();
});
});
});
gives me...
FAIL __tests__/Test.js (6.08s)
● Test › it Tests actions
- Throws: [object Object]
Has anybody made this work?
I figured it out! I just needed to use Jest's own methods for fast-forwarding any timers. i.e. just add the line
jest.runAllTimers();
So the working version of my first example would be
jest.autoMockOff();
describe('Test', function () {
it('Tests actions', function () {
var Reflux = require('../node_modules/reflux/index');
var action = Reflux.createAction('action');
var mockFn = jest.genMockFn();
var store = Reflux.createStore({
init: function () {
this.listenTo(action, this.onAction);
},
onAction: function () {
mockFn();
}
});
action('Hello World');
jest.runAllTimers();
expect(mockFn).toBeCalled();
});
});
I want to test the "addGroup" function using Jasmine. I get the following error:
Error: Expected spy modifyMyHtml to have been called.at null.
I don't know what is the best way to test the addGroup function. Please HELP.....
var myRecord = {
addGroup: function(groupNumber) {
$.when(myRecord.getHtml())
.done(function(returnedHtml){
myRecord.modifyMyHtml(returnedHtml);
});
},
getHtml: function() {
return $.ajax({url: "myHtmlFile.html", dataType: "html" });
},
// adds options and events to my returned HTML
modifyMyHtml: function(returnedHtml) {
$('#outerDiv').html(returnedHtml);
var myOptions = myRecord.getOptions();
$('#optionsField').append(myOptions);
myRecord.bindEventsToDiv();
},
}
====JASMINE TEST
describe("Configure Record page", function() {
var fixture;
jasmine.getFixtures().fixturesPath = "/test/" ;
jasmine.getFixtures().load("myHtmlFile.html");
fixture = $("#jasmine-fixtures").html();
describe("addGroup", function(){
beforeEach(function() {
var groupNumber = 0;
spyOn(myRecord, "getHtml").andCallFake(function(){
return $.Deferred().promise();
});
spyOn(myRecord, "modifyMyHtml");
myRecord.addGroup(groupNumber);
});
it("Should call getHtml", function() {
expect(myRecord.getHtml).toHaveBeenCalled();
});
it("Should call modifyMyHtml", function() {
expect(myRecord.modifyMyHtml).toHaveBeenCalled(); ==>FAILS
});
});
});
You have to resolve the promise before you return em in your andCallFake.
spyOn(myRecord, "getHtml").andCallFake(function(){
return $.Deferred().resolve ().promise();
});
Btw. you should not test that the function on the object you wanna test are called, but that the html in the DOM are set with the right html
it("Should call modifyMyHtml", function() {
spyOn(myRecord, "getHtml").andCallFake(function(){
return $.Deferred().resolveWith(null, 'returnedHtml').promise();
});
expect($('#outerDiv').html).toEqual('returnedHtml')
});