如何使用Jasmine测试具有依赖注入的Angular控制器?

时间:2016-06-20 12:51:43

标签: javascript angularjs jasmine chutzpah

我正在尝试测试我的一个Angular控制器。首先,我想看看,我可以在控制器中调用一个简单的函数,它返回两个数字的总和。

这是控制器的相关部分,具有简单的求和功能(为了简单起见,我已将所有其他功能保留下来):

detailModule.controller('detailController',
    function detailController($rootScope, $scope, detailService, sharedPropertiesService, facebookService, helperService, uiStateService) {

    $scope.sum = function (first, second) {
        return first + second;
    }
});

除了$ rootscope和$ scope之外,控制器还需要几项服务。

现在我想从Jasmine调用这个sum函数,并为此目的制作了这个js。

/// <reference path="D:\Users\bbrinch\Documents\Visual Studio 2015\Projects\Homework\Homework\Scripts/jasmine/jasmine.js" />
/// <reference path="D:\Users\bbrinch\Documents\Visual Studio 2015\Projects\Homework\Homework\Scripts/angular.min.js" />
/// <reference path="D:\Users\bbrinch\Documents\Visual Studio 2015\Projects\Homework\Homework\Scripts/angular-mocks.js" />
/// <reference path="app.js" />
/// <reference path="detailController.js" />

'use strict';

describe('detailModule', function () {
    beforeEach(module('detailModule'));

    describe('detailController', function () {
        var scope, controller, rootScope;
        var detailService, sharedPropertiesService, facebookService, helperService, uiStateService;

        beforeEach(inject(function (_$rootScope, _$scope, _detailService, _sharedPropertiesService, _facebookService, _helperService, _uiStateService, $controller) {
            rootScope = _$rootScope;
            scope = $rootScope.$new();
            detailService = _detailService;
            sharedPropertiesService = _sharedPropertiesService;
            facebookService = _facebookService;
            helperService = _helperService;
            uiStateService = _uiStateService;

            controller = $controller('detailController', {
                $rootScope : rootScope,
                $scope: scope,
                detailService: detailService,
                sharedPropertiesService: sharedPropertiesService,
                facebookService: facebookService,
                helperService: helperService,
                uiStateService: uiStateService
            });
        }));

        it('should return 10', function () {
            var result = scope.sum(3, 7);
            expect(result).toEqual(10);
        });
    })
})

包含的app.js包含detailModule。

我已经安装了chutzpah来运行我的测试。这就是说:

------ Test started: File: D:\Users\bbrinch\Documents\Visual Studio 2015\Projects\Homework\Homework\App\Detail\detailTest.js ------
Test 'detailModule detailController:should return 10' failed
    Error: [$injector:modulerr] http://errors.angularjs.org/1.5.7/$injector/modulerr?p0=detailModule&p1=%5B%24injector%3Amodulerr%5D%20http%3A%2F%2Ferrors.angularjs.org%2F1.5.7%2F%24injector%2Fmodulerr%3Fp0%3DhomeworkModule%26p1%3D%255B%2524injector%253Anomod%255D%2520http%253A%252F%252Ferrors.angularjs.org%252F1.5.7%252F%2524injector%252Fnomod%253Fp0%253DhomeworkModule%250Afile%253A%252F%252F%252FD%253A%252FUsers%252Fbbrinch%252FDocuments%252FVisual%252520Studio%2525202015%252FProjects%252FHomework%252FHomework%252FScripts%252Fangular.min.js%253A25%253A84%250Ab%2540file%253A%252F%252F%252FD%253A%252FUsers%252Fbbrinch%252FDocuments%252FVisual%252520Studio%2525202015%252FProjects%252FHomework%252FHomework%252FScripts%252Fangular.min.js%253A24%253A116%250Afile%253A%252F%252F%252FD%253A%252FUsers%252Fbbrinch%252FDocuments%252FVisual%252520Studio%2525202015%252FProjects%252FHomework%252FHomework%252FScripts%252Fangular.min.js%253A24%253A435%250Afile%253A%252F%252F%252FD%253A%252FUsers%252Fbbrinch%252FDocuments%252FVisual%252520Studio%2525202015%252FProjects%252FHomework%252FHomework%252FScripts%252Fangular.min.js%253A39%253A376%250Ar%2540file%253A%252F%252F%252FD%253A%252FUsers%252Fbbrinch%252FDocuments%252FVisual%252520Studio%2525202015%252FProjects%252FHomework%252FHomework%252FScripts%252Fangular.min.js%253A7%253A359%250Ag%2540file%253A%252F%252F%252FD%253A%252FUsers%252Fbbrinch%252FDocuments%252FVisual%252520Studio%2525202015%252FProjects%252FHomework%252FHomework%252FScripts%252Fangular.min.js%253A39%253A223%250Afile%253A%252F%252F%252FD%253A%252FUsers%252Fbbrinch%252FDocuments%252FVisual%252520Studio%2525202015%252FProjects%252FHomework%252FHomework%252FScripts%252Fangular.min.js%253A39%253A392%250Ar%2540file%253A%252F%252F%252FD%253A%252FUsers%252Fbbrinch%252FDocuments%252FVisual%252520Studio%2525202015%252FProjects%252FHomework%252FHomework%252FScripts%252Fangular.min.js%253A7%253A359%250Ag%2540file%253A%252F%252F%252FD%253A%252FUsers%252Fbbrinch%252FDocuments%252FVisual%252520Studio%2525202015%252FProjects%252FHomework%252FHomework%252FScripts%252Fangular.min.js%253A39%253A223%250Adb%2540file%253A%252F%252F%252FD%253A%252FUsers%252Fbbrinch%252FDocuments%252FVisual%252520Studio%2525202015%252FProjects%252FHomework%252FHomework%252FScripts%252Fangular.min.js%253A43%253A247%250AworkFn%2540file%253A%252F%252F%252FD%253A%252FUsers%252Fbbrinch%252FDocuments%252FVisual%252520Studio%2525202015%252FProjects%252FHomework%252FHomework%252FScripts%252Fangular-mocks.js%253A3067%253A60%250AattemptSync%2540file%253A%252F%252F%252FC%253A%252FUsers%252Fbbrinch%252FAppData%252FLocal%252FMicrosoft%252FVisualStudio%252F14.0%252FExtensions%252F5t5s0xe3.mo1%252FTestFiles%252Fjasmine%252Fv2%252Fjasmine.js%253A1886%253A28%250Arun%2540file%253A%252F%252F%252FC%253A%252FUsers%252Fbbrinch%252FAppData%252FLocal%252FMicrosoft%252FVisualStudio%252F14.0%252FExtensions%252F5t5s0xe3.mo1%252FTestFiles%252Fjasmine%252Fv2%252Fjasmine.js%253A1874%253A20%250Aexecute%2540file%253A%252F%252F%252FC%253A%252FUsers%252Fbbrinch%252FAppData%252FLocal%252FMicrosoft%252FVisualStudio%252F14.0%252FExtensions%252F5t5s0xe3.mo1%252FTestFiles%252Fjasmine%252Fv2%252Fjasmine.js%253A1859%253A13%250AqueueRunnerFactory%2540file%253A%252F%252F%252FC%253A%252FUsers%252Fbbrinch%252FAppData%252FLocal%252FMicrosoft%252FVisualStudio%252F14.0%252FExtensions%252F5t5s0xe3.mo1%252FTestFiles%252Fjasmine%252Fv2%252Fjasmine.js%253A697%253A42%250Aexecute%2540file%253A%252F%252F%252FC%253A%252FUsers%252Fbbrinch%252FAppData%252FLocal%252FMicrosoft%252FVisualStudio%252F14.0%252FExtensions%252F5t5s0xe3.mo1%252FTestFiles%252Fjasmine%252Fv2%252Fjasmine.js%253A359%253A28%250Afn%2540file%253A%252F%252F%252FC%253A%252FUsers%252Fbbrinch%252FAppData%252FLocal%252FMicrosoft%252FVisualStudio%252F14.0%252FExtensions%252F5t5s0xe3.mo1%252FTestFiles%252Fjasmine%252Fv2%252Fjasmine.js%253A2479%253A44%250AattemptAsync%2540file%253A%252F%252F%252FC%253A%252FUsers%252Fbbrinch%252FAppData%252FLocal%252FMicrosoft%252FVisualStudio%252F14.0%252FExtensions%252F5t5s0xe3.mo1%252FTestFiles%252Fjasmine%252Fv2%252Fjasmine.js%253A1916%253A28%250Arun%2540file%253A%252F%252F%252FC%253A%252FUsers%252Fbbrinch%252FAppData%252FLocal%252FMicrosoft%252FVisualStudio%252F14.0%252FExtensions%252F5t5s0xe3.mo1%252FTestFiles%252Fjasmine%252Fv2%252Fjasmine.js%253A1871%253A21%250Aexecute%2540file%253A%252F%252F%252FC%253A%252FUsers%252Fbbrinch%252FAppData%252FLocal%252FMicrosoft%252FVisualStudio%252F14.0%252FExtensions%252F5t5s0xe3.mo1%252FTestFiles%252Fjasmine%252Fv2%252Fjasmine.js%253A1859%253A13%250AqueueRunnerFactory%2540file%253A%252F%252F%252FC%253A%252FUsers%252Fbbrinch%252FAppData%252FLocal%252FMicrosoft%252FVisualStudio%252F14.0%252FExtensions%252F5t5s0xe3.mo1%252FTestFiles%252Fjasmine%252Fv2%252Fjasmine.js%253A697%253A42%250Afn%2540file%253A%252F%252F%252FC%253A%252FUsers%252Fbbrinch%252FAppData%252FLocal%252FMicrosoft%252FVisualStudio%252F14.0%252FExtensions%252F5t5s0xe3.mo1%252FTestFiles%252Fjasmine%252Fv2%252Fjasmine.js%253A2464%253A31%250AattemptAsync%2540file%253A%252F%252F%252FC%253A%252FUsers%252Fbbrinch%252FAppData%252FLocal%252FMicrosoft%252FVisualStudio%252F14.0%252FExtensions%252F5t5s0xe3.mo1%252FTestFiles%252Fjasmine%252Fv2%252Fjasmine.js%253A1916%253A28%250Arun%2540file%253A%252F%252F%252FC%253A%252FUsers%252Fbbrinch%252FAppData%252FLocal%252FMicrosoft%252FVisualStudio%252F14.0%252FExtensions%252F5t5s0xe3.mo1%252FTestFiles%252Fjasmine%252Fv2%252Fjasmine.js%253A1871%253A21%250Aexecute%2540file%253A%252F%252F%252FC%253A%252FUsers%252Fbbrinch%252FAppData%252FLocal%252FMicrosoft%252FVisualStudio%252F14.0%252FExtensions%252F5t5s0xe3.mo1%252FTestFiles%252Fjasmine%252Fv2%252Fjasmine.js%253A1859%253A13%250AqueueRunnerFactory%2540file%253A%252F%252F%252FC%253A%252FUsers%252Fbbrinch%252FAppData%252FLocal%252FMicrosoft%252FVisualStudio%252F14.0%252FExtensions%252F5t5s0xe3.mo1%252FTestFiles%252Fjasmine%252Fv2%252Fjasmine.js%253A697%253A42%250Afn%2540file%253A%252F%252F%252FC%253A%252FUsers%252Fbbrinch%252FAppData%252FLocal%252FMicrosoft%252FVisualStudio%252F14.0%252FExtensions%252F5t5s0xe3.mo1%252FTestFiles%252Fjasmine%252Fv2%252Fjasmine.js%253A2464%253A31%250AattemptAsync%2540file%253A%252F%252F%252FC%253A%252FUsers%252Fbbrinch%252FAppData%252FLocal%252FMicrosoft%252FVisualStudio%252F14.0%252FExtensions%252F5t5s0xe3.mo1%252FTestFiles%252Fjasmine%252Fv2%252Fjasmine.js%253A1916%253A28%250Arun%2540file%253A%252F%252F%252FC%253A%252FUsers%252Fbbrinch%252FAppData%252FLocal%252FMicrosoft%252FVisualStudio%252F14.0%252FExtensions%252F5t5s0xe3.mo1%252FTestFiles%252Fjasmine%252Fv2%252Fjasmine.js%253A1871%253A21%250Aexecute%2540file%253A%252F%252F%252FC%253A%252FUsers%252Fbbrinch%252FAppData%252FLocal%252FMicrosoft%252FVisualStudio%252F14.0%252FExtensions%252F5t5s0xe3.mo1%252FTestFiles%252Fjasmine%252Fv2%252Fjasmine.js%253A1859%253A13%250AqueueRunnerFactory%2540file%253A%252F%252F%252FC%253A%252FUsers%252Fbbrinch%252FAppData%252FLocal%252FMicrosoft%252FVisualStudio%252F14.0%252FExtensions%252F5t5s0xe3.mo1%252FTestFiles%252Fjasmine%252Fv2%252Fjasmine.js%253A697%253A42%250Aexecute%2540file%253A%252F%252F%252FC%253A%252FUsers%252Fbbrinch%252FAppData%252FLocal%252FMicrosoft%252FVisualStudio%252F14.0%252FExtensions%252F5t5s0xe3.mo1%252FTestFiles%252Fjasmine%252Fv2%252Fjasmine.js%253A2326%253A25%250Aexecute%2540file%253A%252F%252F%252FC%253A%252FUsers%252Fbbrinch%252FAppData%252FLocal%252FMicrosoft%252FVisualStudio%252F14.0%252FExtensions%252F5t5s0xe3.mo1%252FTestFiles%252Fjasmine%252Fv2%252Fjasmine.js%253A757%253A24%250AinitializeJasmine%2540file%253A%252F%252F%252FC%253A%252FUsers%252Fbbrinch%252FAppData%252FLocal%252FMicrosoft%252FVisualStudio%252F14.0%252FExtensions%252F5t5s0xe3.mo1%252FTestFiles%252Fjasmine%252Fv2%252Fboot.js%253A122%253A24%250Aonload%2540file%253A%252F%252F%252FD%253A%252FUsers%252Fbbrinch%252FDocuments%252FVisual%252520Studio%2525202015%252FProjects%252FHomework%252FHomework%252FApp%252FDetail%252F_Chutzpah.506d30473962e5a1c0c0767c74fcc633.test.html%253A65%253A45%0Afile%3A%2F%2F%2FD%3A%2FUsers%2Fbbrinch%2FDocuments%2FVisual%2520Studio%25202015%2FProjects%2FHomework%2FHomework%2FScripts%2Fangular.min.js%3A40%3A172%0Ar%40file%3A%2F%2F%2FD%3A%2FUsers%2Fbbrinch%2FDocuments%2FVisual%2520Studio%25202015%2FProjects%2FHomework%2FHomework%2FScripts%2Fangular.min.js%3A7%3A359%0Ag%40file%3A%2F%2F%2FD%3A%2FUsers%2Fbbrinch%2FDocuments%2FVisual%2520Studio%25202015%2FProjects%2FHomework%2FHomework%2FScripts%2Fangular.min.js%3A39%3A223%0Afile%3A%2F%2F%2FD%3A%2FUsers%2Fbbrinch%2FDocuments%2FVisual%2520Studio%25202015%2FProjects%2FHomework%2FHomework%2FScripts%2Fangular.min.js%3A39%3A392%0Ar%40file%3A%2F%2F%2FD%3A%2FUsers%2Fbbrinch%2FDocuments%2FVisual%2520Studio%25202015%2FProjects%2FHomework%2FHomework%2FScripts%2Fangular.min.js%3A7%3A359%0Ag%40file%3A%2F%2F%2FD%3A%2FUsers%2Fbbrinch%2FDocuments%2FVisual%2520Studio%25202015%2FProjects%2FHomework%2FHomework%2FScripts%2Fangular.min.js%3A39%3A223%0Adb%40file%3A%2F%2F%2FD%3A%2FUsers%2Fbbrinch%2FDocuments%2FVisual%2520Studio%25202015%2FProjects%2FHomework%2FHomework%2FScripts%2Fangular.min.js%3A43%3A247%0AworkFn%40file%3A%2F%2F%2FD%3A%2FUsers%2Fbbrinch%2FDocuments%2FVisual%2520Studio%25202015%2FProjects%2FHomework%2FHomework%2FScripts%2Fangular-mocks.js%3A3067%3A60%0AattemptSync%40file%3A%2F%2F%2FC%3A%2FUsers%2Fbbrinch%2FAppData%2FLocal%2FMicrosoft%2FVisualStudio%2F14.0%2FExtensions%2F5t5s0xe3.mo1%2FTestFiles%2Fjasmine%2Fv2%2Fjasmine.js%3A1886%3A28%0Arun%40file%3A%2F%2F%2FC%3A%2FUsers%2Fbbrinch%2FAppData%2FLocal%2FMicrosoft%2FVisualStudio%2F14.0%2FExtensions%2F5t5s0xe3.mo1%2FTestFiles%2Fjasmine%2Fv2%2Fjasmine.js%3A1874%3A20%0Aexecute%40file%3A%2F%2F%2FC%3A%2FUsers%2Fbbrinch%2FAppData%2FLocal%2FMicrosoft%2FVisualStudio%2F14.0%2FExtensions%2F5t5s0xe3.mo1%2FTestFiles%2Fjasmine%2Fv2%2Fjasmine.js%3A1859%3A13%0AqueueRunnerFactory%40file%3A%2F%2F%2FC%3A%2FUsers%2Fbbrinch%2FAppData%2FLocal%2FMicrosoft%2FVisualStudio%2F14.0%2FExtensions%2F5t5s0xe3.mo1%2FTestFiles%2Fjasmine%2Fv2%2Fjasmine.js%3A697%3A42%0Aexecute%40file%3A%2F%2F%2FC%3A%2FUsers%2Fbbrinch%2FAppData%2FLocal%2FMicrosoft%2FVisualStudio%2F14.0%2FExtensions%2F5t5s0xe3.mo1%2FTestFiles%2Fjasmine%2Fv2%2Fjasmine.js%3A359%3A28%0Afn%40file%3A%2F%2F%2FC%3A%2FUsers%2Fbbrinch%2FAppData%2FLocal%2FMicrosoft%2FVisualStudio%2F14.0%2FExtensions%2F5t5s0xe3.mo1%2FTestFiles%2Fjasmine%2Fv2%2Fjasmine.js%3A2479%3A44%0AattemptAsync%40file%3A%2F%2F%2FC%3A%2FUsers%2Fbbrinch%2FAppData%2FLocal%2FMicrosoft%2FVisualStudio%2F14.0%2FExtensions%2F5t5s0xe3.mo1%2FTestFiles%2Fjasmine%2Fv2%2Fjasmine.js%3A1916%3A28%0Arun%40file%3A%2F%2F%2FC%3A%2FUsers%2Fbbrinch%2FAppData%2FLocal%2FMicrosoft%2FVisualStudio%2F14.0%2FExtensions%2F5t5s0xe3.mo1%2FTestFiles%2Fjasmine%2Fv2%2Fjasmine.js%3A1871%3A21%0Aexecute%40file%3A%2F%2F%2FC%3A%2FUsers%2Fbbrinch%2FAppData%2FLocal%2FMicrosoft%2FVisualStudio%2F14.0%2FExtensions%2F5t5s0xe3.mo1%2FTestFiles%2Fjasmine%2Fv2%2Fjasmine.js%3A1859%3A13%0AqueueRunnerFactory%40file%3A%2F%2F%2FC%3A%2FUsers%2Fbbrinch%2FAppData%2FLocal%2FMicrosoft%2FVisualStudio%2F14.0%2FExtensions%2F5t5s0xe3.mo1%2FTestFiles%2Fjasmine%2Fv2%2Fjasmine.js%3A697%3A42%0Afn%40file%3A%2F%2F%2FC%3A%2FUsers%2Fbbrinch%2FAppData%2FLocal%2FMicrosoft%2FVisualStudio%2F14.0%2FExtensions%2F5t5s0xe3.mo1%2FTestFiles%2Fjasmine%2Fv2%2Fjasmine.js%3A2464%3A31%0AattemptAsync%40file%3A%2F%2F%2FC%3A%2FUsers%2Fbbrinch%2FAppData%2FLocal%2FMicrosoft%2FVisualStudio%2F14.0%2FExtensions%2F5t5s0xe3.mo1%2FTestFiles%2Fjasmine%2Fv2%2Fjasmine.js%3A1916%3A28%0Arun%40file%3A%2F%2F%2FC%3A%2FUsers%2Fbbrinch%2FAppData%2FLocal%2FMicrosoft%2FVisualStudio%2F14.0%2FExtensions%2F5t5s0xe3.mo1%2FTestFiles%2Fjasmine%2Fv2%2Fjasmine.js%3A1871%3A21%0Aexecute%40file%3A%2F%2F%2FC%3A%2FUsers%2Fbbrinch%2FAppData%2FLocal%2FMicrosoft%2FVisualStudio%2F14.0%2FExtensions%2F5t5s0xe3.mo1%2FTestFiles%2Fjasmine%2Fv2%2Fjasmine.js%3A1859%3A13%0AqueueRunnerFactory%40file%3A%2F%2F%2FC%3A%2FUsers%2Fbbrinch%2FAppData%2FLocal%2FMicrosoft%2FVisualStudio%2F14.0%2FExtensions%2F5t5s0xe3.mo1%2FTestFiles%2Fjasmine%2Fv2%2Fjasmine.js%3A697%3A42%0Afn%40file%3A%2F%2F%2FC%3A%2FUsers%2Fbbrinch%2FAppData%2FLocal%2FMicrosoft%2FVisualStudio%2F14.0%2FExtensions%2F5t5s0xe3.mo1%2FTestFiles%2Fjasmine%2Fv2%2Fjasmine.js%3A2464%3A31%0AattemptAsync%40file%3A%2F%2F%2FC%3A%2FUsers%2Fbbrinch%2FAppData%2FLocal%2FMicrosoft%2FVisualStudio%2F14.0%2FExtensions%2F5t5s0xe3.mo1%2FTestFiles%2Fjasmine%2Fv2%2Fjasmine.js%3A1916%3A28%0Arun%40file%3A%2F%2F%2FC%3A%2FUsers%2Fbbrinch%2FAppData%2FLocal%2FMicrosoft%2FVisualStudio%2F14.0%2FExtensions%2F5t5s0xe3.mo1%2FTestFiles%2Fjasmine%2Fv2%2Fjasmine.js%3A1871%3A21%0Aexecute%40file%3A%2F%2F%2FC%3A%2FUsers%2Fbbrinch%2FAppData%2FLocal%2FMicrosoft%2FVisualStudio%2F14.0%2FExtensions%2F5t5s0xe3.mo1%2FTestFiles%2Fjasmine%2Fv2%2Fjasmine.js%3A1859%3A13%0AqueueRunnerFactory%40file%3A%2F%2F%2FC%3A%2FUsers%2Fbbrinch%2FAppData%2FLocal%2FMicrosoft%2FVisualStudio%2F14.0%2FExtensions%2F5t5s0xe3.mo1%2FTestFiles%2Fjasmine%2Fv2%2Fjasmine.js%3A697%3A42%0Aexecute%40file%3A%2F%2F%2FC%3A%2FUsers%2Fbbrinch%2FAppData%2FLocal%2FMicrosoft%2FVisualStudio%2F14.0%2FExtensions%2F5t5s0xe3.mo1%2FTestFiles%2Fjasmine%2Fv2%2Fjasmine.js%3A2326%3A25%0Aexecute%40file%3A%2F%2F%2FC%3A%2FUsers%2Fbbrinch%2FAppData%2FLocal%2FMicrosoft%2FVisualStudio%2F14.0%2FExtensions%2F5t5s0xe3.mo1%2FTestFiles%2Fjasmine%2Fv2%2Fjasmine.js%3A757%3A24%0AinitializeJasmine%40file%3A%2F%2F%2FC%3A%2FUsers%2Fbbrinch%2FAppData%2FLocal%2FMicrosoft%2FVisualStudio%2F14.0%2FExtensions%2F5t5s0xe3.mo1%2FTestFiles%2Fjasmine%2Fv2%2Fboot.js%3A122%3A24%0Aonload%40file%3A%2F%2F%2FD%3A%2FUsers%2Fbbrinch%2FDocuments%2FVisual%2520Studio%25202015%2FProjects%2FHomework%2FHomework%2FApp%2FDetail%2F_Chutzpah.506d30473962e5a1c0c0767c74fcc633.test.html%3A65%3A45 in file:///D:/Users/bbrinch/Documents/Visual%20Studio%202015/Projects/Homework/Homework/Scripts/angular.min.js (line 40)
    r@file:///D:/Users/bbrinch/Documents/Visual%20Studio%202015/Projects/Homework/Homework/Scripts/angular.min.js:7:359
    g@file:///D:/Users/bbrinch/Documents/Visual%20Studio%202015/Projects/Homework/Homework/Scripts/angular.min.js:39:223
    db@file:///D:/Users/bbrinch/Documents/Visual%20Studio%202015/Projects/Homework/Homework/Scripts/angular.min.js:43:247
    workFn@file:///D:/Users/bbrinch/Documents/Visual%20Studio%202015/Projects/Homework/Homework/Scripts/angular-mocks.js:3067:60
    attemptSync@file:///C:/Users/bbrinch/AppData/Local/Microsoft/VisualStudio/14.0/Extensions/5t5s0xe3.mo1/TestFiles/jasmine/v2/jasmine.js:1886:28
    TypeError: undefined is not an object (evaluating 'scope.sum') in file:///D:/Users/bbrinch/Documents/Visual%20Studio%202015/Projects/Homework/Homework/App/Detail/detailTest.js (line 37)
    attemptSync@file:///C:/Users/bbrinch/AppData/Local/Microsoft/VisualStudio/14.0/Extensions/5t5s0xe3.mo1/TestFiles/jasmine/v2/jasmine.js:1886:28
    run@file:///C:/Users/bbrinch/AppData/Local/Microsoft/VisualStudio/14.0/Extensions/5t5s0xe3.mo1/TestFiles/jasmine/v2/jasmine.js:1874:20
    execute@file:///C:/Users/bbrinch/AppData/Local/Microsoft/VisualStudio/14.0/Extensions/5t5s0xe3.mo1/TestFiles/jasmine/v2/jasmine.js:1859:13
    queueRunnerFactory@file:///C:/Users/bbrinch/AppData/Local/Microsoft/VisualStudio/14.0/Extensions/5t5s0xe3.mo1/TestFiles/jasmine/v2/jasmine.js:697:42
    execute@file:///C:/Users/bbrinch/AppData/Local/Microsoft/VisualStudio/14.0/Extensions/5t5s0xe3.mo1/TestFiles/jasmine/v2/jasmine.js:359:28
in D:\Users\bbrinch\Documents\Visual Studio 2015\Projects\Homework\Homework\App\Detail\detailTest.js (line 36)

0 passed, 1 failed, 1 total (chutzpah).

========== Total Tests: 0 passed, 1 failed, 1 total ==========

我不知道我在这里缺少什么。

更新

我试图按照estus的建议简化控制器:

现在看起来像这样:

/// <reference path="D:\Users\bbrinch\Documents\Visual Studio 2015\Projects\Homework\Homework\Scripts/jasmine/jasmine.js" />
/// <reference path="D:\Users\bbrinch\Documents\Visual Studio 2015\Projects\Homework\Homework\Scripts/angular.min.js" />
/// <reference path="D:\Users\bbrinch\Documents\Visual Studio 2015\Projects\Homework\Homework\Scripts/angular-mocks.js" />
/// <reference path="app.js" />
/// <reference path="detailController.js" />

'use strict';

describe('detailModule', function () {
    beforeEach(module('detailModule'));

    describe('detailController', function () {
        var scope, controller;

        beforeEach(inject(function (_$rootScope) {
            scope = _$rootScope.$new();

            controller = $controller('detailController', {
                $scope: scope,
            });
        }));

        it('should return 10', function () {
            var result = scope.sum(3, 7);
            expect(result).toEqual(10);
        });
    })
})

但是我仍然会收到此错误:

由于以下原因导致无法实例化模块detailModule: [$ injector:modulerr] http://errors.angularjs.org/1.5.7/ $ injector / modulerr?p0 = h ...

更新2

我解决了。问题是我缺少参考文献。基本上所有模块,服务,控制器等都必须以一种方式引用。

如果它可以自动遍历并解析所有需要的引用,那将会很好。

2 个答案:

答案 0 :(得分:2)

可以跟踪

Error stack,并发送消息says the initial reason

  

模块&#39; homeworkModule&#39;不可用!您要么错误拼写了模块名称,要么忘记加载它。如果注册模块,请确保将依赖项指定为第二个参数。

$controller与当前规范共享注入器,没有理由为$controller指定依赖项,除非它们是本地的或模拟的。它应该是:

        controller = $controller('detailController', {
            $scope: scope
        });

答案 1 :(得分:0)

  

TypeError:undefined不是对象(评估'scope.sum')

这意味着范围未定义。

这很可能是因为这段代码,

rootScope = _$rootScope;
scope = $rootScope.$new(); // it should be either _$rootScope.new() or rootScope.new()

只需更改此内容即可查看。