I am currently trying jasmine-node to unit test my Titanium app. I am open to suggestions about switching to an other unit-testing framework if it can solve my problem but first, here is my question.
My installation of jasmine-node is working, I can perform very simple tests like this one :
var util = require('../app/controllers/utils.js');
describe("util test", function(){
it('should compute the sum between 1 & 2', function(){
var sum = util.computeSum(1, 2);
expect(sum).toEqual(3);
});
});
The above code tests the following function and works as expected.
exports.computeSum = function(a,b) {
return a+b;
};
When I try to test some code which call the Ti module though, it fails, saying "Ti is not defined".
describe("Ti.UI",function(){
it("create custom alert", function(){
var view = util.displayCustomAlert("title", "message");
should(view).not.be.null;
});
});
Above function is tested by the following test :
exports.displayCustomAlert = function(customTitle, customMessage){
return Ti.UI.createAlertDialog({
title:customTitle,
message:customMessage
});
};
How can I make jasmine-node work with Titanium ?
I'd recommend using TiShadow to run Jasmine tests or TiO2 to run Mocha tests for Titanium apps.
Related
I want to write my tests using the mocha framework i.e
describe('',function(){
it('', function()){
});
});
Can this be done for my intern tests, does intern have a mocha test runner?
It sounds like you want to use a mocha-like interface rather than necessarily using mocha itself. If that's the case, Intern offers the bdd interface. You could do something like:
var bdd = require('intern!bdd');
var describe = bdd.describe;
var it = bdd.it;
describe('', function(){
it('', function(){
});
});
I'm trying to get a basic unit test example working. It all works fine with this app.js
var whapp = angular.module('whapp', [])
.filter('reverse',[function(){
return function(string){
return string.split('').reverse().join('');
}
}]);
and this spec.js
describe('Filters', function(){ //describe your object type
beforeEach(module('whapp')); //load module
describe('reverse',function(){ //describe your app name
var reverse, rootScope;
beforeEach(inject(function($filter){ //initialize your filter
reverse = $filter('reverse',{});
}));
it('Should reverse a string', function(){ //write tests
expect(reverse('rahil')).toBe('lihar'); //pass
});
});
});
with this karma files config
files: [
'node_modules/angular/angular.js',
'node_modules/angular-mocks/angular-mocks.js',
'node_modules/angular-mocks/angular-route/angular-route.js',
'node_modules/angular-mocks/angular-ui-router/release/angular-ui-router.js',
'app/js/*.js',
'tests/*.js'
]
The problem occurs when I try to inject ngRoute into my module in app.js like so
var whapp = angular.module('whapp', ['ngRoute'])
.filter('reverse',[function(){
return function(string){
return string.split('').reverse().join('');
}
}]);
In which case I get the following error in karma [UPDATE: this error occurs even if I don't load the angular-mock.js library into karma as shown above]
TypeError: undefined is not a constructor (evaluating 'reverse('rahil')') in tests/spec.js (line 9)
So... how do I inject ngRoute into spec.js correctly? I've tried a variety of things, none of which worked.
Apparently, you get this error because PhantomJS fails to instantiate your main Angular module whapp. One possible reason is, that the file node_modules/angular-mocks/angular-route/angular-route.js is missing.
Obviously, you are using npm to manage your dependencies. So try to replace your current file with:
node_modules/angular-route/angular-route.js
The same for the ui-route module:
node_modules/angular-ui-router/release/angular-ui-router.js
I hope this will help you.
I am new to Mocha and AngularJS Unit Testing but want to test my application using Mocha. I have basic language tests working, but I cannot run tests against my applications Factory or Controller.
I have the following basic files.
apps.js
aangular.module('MyApp', []);
file1.js
angular.module('MyApp').factory('Factory1' ...);
file2.js
angular.module('MyApp').factory('Factory2' ...);
angular.module('MyApp').factory('Controller' ...);
describe('Main Test', function() {
var FactoryToTest;
beforeEach(module('MyApp'));
beforeEach(inject(function (_Factory_) {
FactoryToTest = _Factory_;
}));
describe('Factory2', function () {
it('should return "unknown"', function () {
Game = {};
expect(new Factory2(Game)).to.equal('unknown');
});
});
});
When I run the test, it generates an error, and I am not sure what to fix to get this to work.
Error:
Message:
object is not a function
Stack:
TypeError: object is not a function
at Suite.<anonymous> (b:\app\test.js:5:16)
You're getting an error because the beforeEach function should take a callback function instead of an object. According to the Angular guide on module unit testing (scroll to bottom of the page) :
Each module can only be loaded once per injector. Usually an Angular app has only one injector and modules are only loaded once. Each test has its own injector and modules are loaded multiple times.
I'm trying to use the methods beforeAll and afterAll of jasmine, to create a suite of tests with frisby.js, because actually, frisby doesn't have a support for this methods. So, this is what I'm trying to do:
var frisby = require('frisby');
describe("setUp and tearDown", function(){
beforeAll(function(){
console.log("test beforeAll");
});
afterAll(function(){
console.log("afterAll");
});
//FRISBY TESTS
}); //end of describe function
If I change the methods before/afterAll to before/afterEach, is working, but when I'm using before/afterAll this error appears on console:
Message:
ReferenceError: beforeAll is not defined
Stacktrace:
ReferenceError: beforeAll is not defined
I have the jasmine version 2.3.2 installed on my project, so, I don't know what I need to do to integrate this method.
Use the jasmine library not the jasmine-node library. The second one does not support beforeAll and afterAll methods.
1- npm install -g jasmine
2- jasmine init
3- write the test in the spec folder:
describe("A spec using beforeAll and afterAll", function() {
var foo;
beforeAll(function() {
foo = 1;
});
afterAll(function() {
foo = 0;
});
it("sets the initial value of foo before specs run", function() {
expect(foo).toEqual(1);
foo += 1;
});
it("does not reset foo between specs", function() {
expect(foo).toEqual(2);
});
});
4- Run the tests --> jasmine
The current version of frisby doesnt suport this kind of setup. The community, like myself is eager to this feature like in this issue describes.
The team is working on this feature, but it will come in version 2 of the package that is in the way for more than a year now. More info at this link.
I'm new at node.js and the framework Mocha for unit testing, but I've created a couple of tests in cloud9 IDE just to see how it works. The code looks like this:
var assert = require("assert");
require("should");
describe('Array', function(){
describe('#indexOf()', function(){
it('should return -1 when the value is not present', function(){
assert.equal(-1, [1,2,3].indexOf(5));
assert.equal(-1, [1,2,3].indexOf(0));
});
});
});
describe('Array', function(){
describe('#indexOf()', function(){
it('should return the index when the value is present', function(){
assert.equal(1, [1,2,3].indexOf(2));
assert.equal(0, [1,2,3].indexOf(1));
assert.equal(2, [1,2,3].indexOf(3));
});
});
});
The tests work if I type mocha in the console, but the IDE shows warnings in the lines where "describe" and "it" are because it says that the variable has not been declared ("undeclared variable").
I wonder what should I do these tests to avoid the warnings.
Thanks.
In cloud9 you can add a hint for globals as a comment at the top of the file and it will remove the warnings.
e.g.
**/* global describe it before */**
var expect = require('chai').expect;
describe('Array', function(){
describe('#indexOf()', function(){
it('should return -1 when the value is not present', function(){
expect(true).to.equal(true);
})
})
})
That's because mocha "executable" wraps your test in requires needed to use mocha functions (describe, and it). Take a look at mocha and _mocha in your node_modules/mocha/bin directory.
On the other hand cloud9 tries to resolve all symbols using a pure node executable, so you have to require everything by hand.