mardi 28 juillet 2015

Injection error when using Jasmine with AngularJS

I've been following the example of this developer for how to setup some basic Jasmine tests with AngularJS here

My problem seems to be that across my entire app, I am using one module which I refer to as myApp. However in my tests it complains about missing references.

app.js

(function () {
    "use strict";

    var myAppModule = angular.module('myApp', ['ngAnimate', 'ngRoute', 'ui.router', 'ngResource']);
})();

dogService,js

(function () {
        "use strict";

        var myAppModule = angular.module('myApp');
        myAppModule.factory('Dog', function () {

        var dogs = [
            { type: "Labrador", name: "Rover" },
            { type: "Tibetan Terrier", name: "Joey" },
            { type: "Yorkshire Terrier", name: "Rufus" }
        ];

        return {
            query: function () {
                return dogs;
            },
            add: function (dog) {
                dogs.push(dog);
            }
        };
    });
}());

serviceSpec.js

///<reference path="~/Scripts/jasmine.js"/>
///<reference path="~/Scripts/jasmine-html.js"/>
///<reference path="~/Scripts/boot.js"/>
///<reference path="~/Scripts/angular.js"/>
///<reference path="~/Scripts/angular-mocks.js"/>
///<reference path="~/Scripts/App/app.js"/>
///<reference path="~/Scripts/App/Services/dogService.js"/>
"use strict";
describe("dogService", function () {

    beforeEach(module("myApp"));

    describe("Dog service", function () {

        var dog;

        beforeEach(inject(function ($injector) {
            dog = $injector.get('Dog');
        }));

        it('should return 3 dogs when querying', function () {
            expect(dog.query().length).toBe(3);
        });

        it('should return 4 dogs when querying after adding a dog', function () {
            dog.add({ name: 'Fido', type: 'German Shepherd' });
            expect(dog.query().length).toBe(4);
        });
    });
});

Finally my error:

finished in 0.071s2 specs, 2 failuresSpec List | FailuresSpec List | Failures 

dogService Dog service should return 3 dogs when querying

Error: [$injector:modulerr] Failed to instantiate module myApp due to: Error: [$injector:modulerr] Failed to instantiate module ngAnimate due to: Error: [$injector:nomod] Module 'ngAnimate' is not available! You either misspelled the module name or forgot to load it.

If I change app.js to this, I won't get any errors, however I am going to have to inject dependencies at a top level in the module.

(function () {
        "use strict";

        var myAppModule = angular.module('myApp', []);
    })();

Does anyone have any advice?

Aucun commentaire:

Enregistrer un commentaire