THE ISSUE:
In an attempt to create unit tests for my Angular application, I set up a basic test app and wrote a simple unit test. However, the test is not functioning as expected.
APPLICATION CODE:
var app = angular.module( 'myApp', [] );
app.controller('InitCtrl', function( $scope, Person )
{
$scope.test_person = Person.Person("Tom");
})
app.factory('Person', function(){
return {
Person: function(name){
this.name = name;
console.log ( name );
}
}
})
UNIT TEST CODE:
describe('Person', function () {
var Person;
beforeEach(module('myApp'));
beforeEach(inject(function (_Person_) {
Person = _Person_;
}));
describe('Constructor', function () {
it('assigns a name', function () {
expect(new Person('Ben')).to.have.property('name', 'Ben');
});
});
});
ERROR MESSAGE DISPLAYED:
Upon running the test, I received the following error message:
PhantomJS 1.9.8 (Mac OS X 0.0.0) Person "before each" hook: workFn for "assigns a name" FAILED
Error: [$injector:modulerr] Failed to instantiate module myApp due to:
Error: [$injector:nomod] Module 'myApp' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
http://errors.angularjs.org/1.4.4/$injector/nomod?p0=myApp
EFFORTS MADE TO RESOLVE:
I attempted using different syntax such as:
var app = angular.module( 'myApp' );
However, this did not solve the issue.
QUESTION AT HAND:
What could be incorrect in this simple test?
Is something missing from the application, or is there an error in the test script?
Thank you!