将 Jasmine 与 AngularJS 一起使用时出现注入错误

Injection error when using Jasmine with AngularJS

我一直在关注此开发人员的示例,了解如何使用 AngularJS here

设置一些基本的 Jasmine 测试

我的问题似乎是在我的整个应用程序中,我使用的是一个模块,我称之为 myApp。但是在我的测试中,它抱怨缺少引用。

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

最后我的错误:

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.

如果我将 app.js 更改为此,我将不会收到任何错误,但是我将不得不在模块的顶层注入依赖项。

(function () {
        "use strict";

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

有人有什么建议吗?

缺少对注入 myApp 的 angular 文件的引用。

///<reference path="~/Scripts/angular-animate.js"/>
///<reference path="~/Scripts/angular-route.js"/>
///<reference path="~/Scripts/angular-ui-router.js"/>
///<reference path="~/Scripts/angular-resource.js"/>

添加这些修复了它,但这只是一个变通解决方案,因为它需要为每个测试脚本添加很多引用。