简体   繁体   中英

Angular Js: Simple unit test not working

THE SITUATION:

I am trying to set up unit test for my angular app. I have created a basic test angular app and wrote a simple unit test, but is not working as expected.

THE APP:

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 );
        }
    }               
})

THE UNIT TEST:

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');
    });

  });

});

THE ERROR MESSAGE:

When i run the test i get the actual 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

THE ATTEMPTS:

I have tried with different sintax as:

var app = angular.module( 'myApp' );

But it doesn't solve the problem.

THE QUESTION:

What is wrong in this simple test?
There is something missing in the app, or something wrong in the test?

Thank you!

Most probably you did not load your app.js in your karma configuration file :

module.exports = function(config) {
    config.set({
        basePath: '',    
        files: [
            '../../path/to/app.js',
         //...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM