我有一个控制器,可以按如下方式设置变量:
terminalController.controller('GeneralSettingsController', [ '$rootScope', '$scope', 'GeneralSettingsService', 'getGeneralSettings',
function($rootScope, $scope, GeneralSettingsService, getGeneralSettings) {
getGeneralSettings.get().$promise.then(function (response) {
$scope.connectionsettings = response;
if(response.screenSaverOn == 'true') {
$scope.screenSaver = 'On';
} else {
$scope.screenSaver = 'Off';
}
});
}
]);
现在我想测试一下,如果响应有一个screenSaverOn =='true',那么$ scope.screenSaver将是'On'
这是我的测试片段:
describe('GeneralSettingsController', function() {
var rootScope, getConnectionSettings, resp;
beforeEach(inject(function($controller,
$rootScope, _$httpBackend_) {
rootScope = $rootScope.$new();
$httpBackend = _$httpBackend_;
$controller('GeneralSettingsController', {
$rootScope : rootScope
});
$httpBackend.when('GET', 'ws/Admin/terminal/settings').respond(
{ response : {
"screenSaverOn" : true
}
});
}));
it('should call get connection settings when page is loaded', function() {
$httpBackend.flush();
assert.equal(rootScope.connectionsettings.response.screenSaverOn, true);
assert.equal(rootScope.screenSaver, 'On');
});
第一个断言成功,第二个失败,我做错了什么?
答案 0 :(得分:0)
问题是您没有将所需的服务注入控制器。它正在调用getGeneralSettings.get()
但是从不注入getGeneralSettings,因此这会导致错误。你只提供$ rootScope。缺少$ scope,GeneralSettingsService,getGeneralSettings。
您必须提供自己的模拟对象,只需直接响应您想要的响应。并删除HTTP后端模拟,因为您没有使用$ http。