我的规范中的变量homecontroller是未定义的,即使我使用的是正确的模块和正确的控制器。我的代码是
describe('HomeController Tests', function () {
//var $translateProvider;
beforeEach(module('myModule'), function ($provide, $translateProvider) {
$provide.factory('customLoader', function ($q) {
return function () {
var deferred = $q.defer();
deferred.resolve({});
return deferred.promise;
};
});
$translateProvider.useLoader('customLoader');
});
var homecontroller, scope;
beforeEach(inject(function ($rootScope, $controller, $httpBackend, SessionStorageService) {
SessionStorageService.Set("LoginResponse", { SessionID: 'h5' });
scope = $rootScope.$new();
homecontroller = $controller('HomeController',
{
$scope: scope
});
$httpBackend.when('GET', 'Translations/locale-en.json?lang=en').respond(200);
}));
类似于这种方式我为同一个模块为另一个控制器编写了一个spec文件,但它工作正常。该规范控制器代码是
describe('LoginController Tests', function () {
var $translateProvider;
beforeEach(module('myModule'), function ($provide, $translateProvider) {
$provide.factory('customLoader', function ($q) {
return function () {
var deferred = $q.defer();
deferred.resolve({});
return deferred.promise;
};
});
$translateProvider.useLoader('customLoader');
});
beforeEach(inject(function ($httpBackend, $controller, SessionStorageService, $injector, $window, Route, _$translateStaticFilesLoader_, $translate, HttpRequestSoapAPI) {
RouteMock = $injector.get("Route");
SessionStorageService.Set("LoginResponse", {SessionID : 'h5'});
logincontroller = $controller('LoginController',
{
//some services here
});
$httpBackend.when('GET', 'Translations/locale-en.json?lang=en').respond(200);
}));
我得到了这个logincontroller很漂亮,但我没有得到那个homecontroller。
注意:两个控制器js文件在主页面中可用,最初登录页面加载了loginController,从那里导航到带有homeController的主页。
请有任何想法