我正在尝试为我的应用中的控制器编写一个简单的测试。控制器使用服务发送AJAX请求(即我真的在测试控制器和服务)。 我正在使用Jasmine(我是第一次使用),而且我也试图接近以下示例形式的Angular文档:
https://docs.angularjs.org/api/ngMock/service/ $ httpBackend
当我运行测试时,我收到以下错误:
Error: [ng:areq] Argument 'LoginController' is not a function, got undefined http://errors.angularjs.org/1.2.16/ng/areq?p0=LoginController&p1=not%20a%20function%2C%20got%20undefined
at http://localhost:8080/FreelancerDB_GUI/js/angular.js:78:12
at assertArg (http://localhost:8080/FreelancerDB_GUI/js/angular.js:1443:11)
at assertArgFn (http://localhost:8080/FreelancerDB_GUI/js/angular.js:1453:3)
at http://localhost:8080/FreelancerDB_GUI/js/angular.js:7131:9
at createController (http://localhost:8080/FreelancerDB_GUI/app/unittests/spec/loginSpec.js:37:19)
at Object.<anonymous> (http://localhost:8080/FreelancerDB_GUI/app/unittests/spec/loginSpec.js:48:27)
at attemptSync (http://localhost:8080/FreelancerDB_GUI/app/unittests/lib/jasmine-2.0.3/jasmine.js:1620:12)
at QueueRunner.run (http://localhost:8080/FreelancerDB_GUI/app/unittests/lib/jasmine-2.0.3/jasmine.js:1608:9)
at QueueRunner.execute (http://localhost:8080/FreelancerDB_GUI/app/unittests/lib/jasmine-2.0.3/jasmine.js:1595:10)
at Spec.Env.queueRunnerFactory (http://localhost:8080/FreelancerDB_GUI/app/unittests/lib/jasmine-2.0.3/jasmine.js:508:35)
进一步说:
Error: Unsatisfied requests: POST http://localhost:8080/FreelancerDB_desktop/services/authentication/login?mandator=Devoteam&password=ae94be3cd532ce4a025884
我认为第二个错误只会发生,因为事情会事先破裂......
我的问题似乎是我以不同于示例中的方式定义控制器。该示例将控制器写为一个简单的函数,而我用以下方式定义它:
angular.module('CVGenAppControllers')
.controller('LoginController', ['$scope','$http','$state','UserService', '$translate','LoginService',
function($scope, $http, $state, userService, $translate, loginService) {
$scope.login = function() {
...
loginService.login(params).then(success, error);
};
function success(p){
...
}
function error(p){
...
}
$scope.init = function () {
...
};
$scope.init();
}
]
);
这也是我的实际测试规范:
describe('LoginControllerTest', function() {
var $httpBackend, $rootScope, createController, authRequestHandler;
module('CVGenAppControllers');
beforeEach(inject(function($injector) {
$httpBackend = $injector.get('$httpBackend');
testurl = server + '/authentication/login' + '?mandator=Devoteam&password=ae94be3cd532ce4a025884';
authRequestHandler = $httpBackend.when('POST', testurl)
.respond(
{ "status":true,
"message":"Login success",
"content":{
"uuid":"db8a6091-0c31-11e4-91f1-546318596611",
"identityName":"Manager",
"firstName":"Manfred",
"lastName":"Manager",
"mandator":"Devoteam",
"roles":["freelancerDB-Admin-Role","UM-Manager-Role"],
"rights":["um_disable_user_own_mandator","um_enable_user_any_mandator","um_write_user_own_mandator","um_enable_user_own_mandator","um_read_mandators_with_users","um_read_own_mandator_with_users","um_write_user_any_mandator","um_read_mandators_with_roles","um_write_any_mandator","um_disable_user_any_mandator","um_change_password","um_write_role_any_mandator","cvg_r_personaldata_owndata"]
}
}
);
$rootScope = $injector.get('$rootScope');
var $controller = $injector.get('$controller');
createController = function() {
return $controller('LoginController', {'$scope' : $rootScope });
};
}));
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
it('should fetch authentication token', function() {
$httpBackend.expectPOST(testurl);
var controller = createController();
$httpBackend.flush();
});
});
我已经查看了一些以前提出过的问题,其中一些问题似乎除了这里描述的问题之外还处理路由问题,对不起,如果这是一个重复的问题。 我应该如何编写测试以及如何声明控制器来解决问题? 任何有关如何解决这个问题的帮助将不胜感激。提前谢谢。