我在哪里出错了我很困惑。我已在module
中添加beforeEach
,似乎仍显示错误消息Module 'appModule' is not available! You either misspelled the module name
这是我的规格:
C0001Spec.js
describe("Controller : c0001Controller", function(){
beforeEach(angular.module('appModule'));
var $rootScope, $httpBackend, httpHandler, createController, appModuleC0001;
beforeEach(inject(function($injector){
$httpBackend = $injector.get('$httpBackend');
appModuleC0001 = $injector.get('appModule.c0001Controller');
httpHandler = $httpBackend.when('POST', 'C0001Login')
.respond({user_id : 'except'}, {password : 'except'});
$rootScope = $injector.get('$rootScope');
var $controller = $injector.get('$controller');
createController = function(){
return $controller("c0001Controller", { '$scope' : $rootScope});
};
}));
afterEach(function(){
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
it("should see the Controller", function(){
expect(appModuleC0001).toBeDefined();
});
it("should use specs.js", function(){
expect(true).toBe(true);
});
it("should show Incorrect Credentials in alert", function(){
$httpBackend.expectPOS('C0001Login');
var controller = createController();
$httpBackend.flush();
expect($rootScope.success).toEqual('error');
});
});
Karma.conf.js
// list of files / patterns to load in the browser
files: [
'node_modules/angular/angular.js',
'node_modules/angular-mocks/angular-route.js',
'node_modules/angular-mocks/angular-mocks.js',
'WebContent/js/controllers/C0001Ctrl.js',
'tests/C0001Spec.js'
],
这是 C0001Ctrl.js
var appModule = angular.module('appModule', []);
appModule.controller('c0001Controller', function($http, $window) {
var user = {};
this.c0001Data = user;
this.submitForm = function() {
if(!angular.isUndefined(this.c0001Data.user_id) && !angular.isUndefined(this.c0001Data.password))
{
var promise = $http.post('C0001Login', user);
promise.success(function(data, status, headers, config) {
if(data.message == 'error'){
alert('Invalid Username/Password');
} else {
$window.location.href = data.url + "#/c0003";
}
});
promise.error(function(data, status, headers, config) {
alert("Invalid Username/Password");
});
}
else {
alert ("Invalid/ Username/password");
}
};
});