0

我不知道这里发生了什么。我正在尝试模拟服务并将其注入控制器中。

我的第一个测试通过了,但是我的第二个测试给出了以下错误:

Error: [ng:areq] http://errors.angularjs.org/1.2.28/ng/areq?p0=authenticationController&p1=not%20a%20function%2C%20got%20undefined
        at Db (/Users/PrimeByDesign/Documents/Development/ADashOfPanache/framework/src/libs/bower/angular/angular.min.js:19)
        at Ya (/Users/PrimeByDesign/Documents/Development/ADashOfPanache/framework/src/libs/bower/angular/angular.min.js:19)
        at /Users/PrimeByDesign/Documents/Development/ADashOfPanache/framework/src/libs/bower/angular/angular.min.js:67
        at /Users/PrimeByDesign/Documents/Development/ADashOfPanache/framework/src/js/functionality/authentication/authenticationSpec.js:51
        at d (/Users/PrimeByDesign/Documents/Development/ADashOfPanache/framework/src/libs/bower/angular/angular.min.js:35)
        at workFn (/Users/PrimeByDesign/Documents/Development/ADashOfPanache/framework/src/libs/bower/angular-mocks/angular-mocks.js:2177)
    Expected h({ $$childTail: null, $$childHead: null, $$nextSibling: null, $$watchers: null, $$listeners: Object({  }), $$listenerCount: Object({  }), $id: '004', $$childScopeClass: null, this: <circular reference: Object>, $parent: h({ $id: '003', $$childTail: <circular reference: Object>, $$childHead: <circular reference: Object>, $$prevSibling: null, $$nextSibling: null, $$watchers: null, $parent: null, $$phase: null, $root: <circular reference: Object>, this: <circular reference: Object>, $$destroyed: false, $$asyncQueue: [  ], $$postDigestQueue: [  ], $$listeners: Object({  }), $$listenerCount: Object({  }), $$isolateBindings: Object({  }), $$childScopeClass: Function }), $$prevSibling: null }) to be null.
        at /Users/PrimeByDesign/Documents/Development/ADashOfPanache/framework/src/js/functionality/authentication/authenticationSpec.js:79

代码如下(authenticationSpec.js):

var mockAuthenticationService = "";
var $controllerConstructor = "";
var scope = {};
var ctrl = {};
    describe("Authentication Controller", function () {


        //you need to indicate your module in a test
        beforeEach(module('app'));

        // before each test inject the following
        beforeEach(inject(function($controller, $rootScope){

            // create new controller and root scope.
            $controllerConstructor  = $controller;
            scope  = $rootScope.$new();

            // Create the Mock authentication service.
            angular.module('app', function($provide) {
                $provide.service('authenticationService', mockAuthenticationService);
            });


            mockAuthenticationService = {};

            //setup the fake service data for a successful login.
            mockAuthenticationService.data = {
                    id: 1,
                    user: { id:1, role:'admin'}
                };

            // create a fake login implementation on our mock object
            mockAuthenticationService.login = function (credentials) {

                // setup result as null.
                var result = null;

                // simulate remote server authenticating.
                if (credentials.username == 'kiran' && credentials.password == 'kiranPassword') {
                    result =  this.data;
                }

                return result;
            };

            ctrl = $controllerConstructor('authenticationController', { $scope: scope, authenticationService: mockAuthenticationService});

        }));



        it("will retrieve user data for the correct credentials", function(){

            var credentials = {
                username: 'kiran',
                password: 'kiranPassword'
            };
            var resultData = scope.login(credentials);

            expect(resultData.id).toEqual(1);
            expect(resultData.user.role).toEqual('admin');

        });


        it('will return null for a user with incorrect credentials', function(){

            var credentials = {
                username: 'BADGUY!',
                password: 'HAXOR'
            };

            var resultData = scope;
            expect(resultData).toBeNull();
        });
    });

我还有测试文件声明:

var testFiles = [
    'framework/src/libs/bower/angular/angular.min.js',
    'framework/src/libs/bower/angular-route/angular-route.min.js',
    'framework/src/libs/bower/angular-mocks/angular-mocks.js',
    'framework/src/js/app.js',
    'framework/src/js/functionality/authentication/*Service.js',
    'framework/src/js/functionality/authentication/*Controller.js',
    'framework/src/js/functionality/authentication/*Spec.js'
];

据我所见,上面的测试文件数组看起来是正确的。

我对Angular真的很陌生,我一直在努力解决它,所以我很感激你能给我的任何建议。

4

0 回答 0