Angularjs 角度单元测试中的模拟$location服务
我对angular的开发是新手,我正在尝试学习如何测试angular控制器。我正在测试的控制器使用$location.seach()。我查看了$location的文档,但没有很快看到我应该如何在karma/jasmine中模拟这一点 控制员:Angularjs 角度单元测试中的模拟$location服务,angularjs,jasmine,Angularjs,Jasmine,我对angular的开发是新手,我正在尝试学习如何测试angular控制器。我正在测试的控制器使用$location.seach()。我查看了$location的文档,但没有很快看到我应该如何在karma/jasmine中模拟这一点 控制员: rmtg.controller('ErrorCtrl', ['Session', '$location', '$routeParams', '$scope', '$window', function(Session, $location, $rou
rmtg.controller('ErrorCtrl', ['Session', '$location', '$routeParams', '$scope', '$window',
function(Session, $location, $routeParams, $scope, $window) {
console.log('ErrorCtrl(%o, %o, %o)', $location.path(), $location.search(), $routeParams);
$scope.status = $location.search().status;
$scope.message = $location.search().message;
$scope.isAuthorized = (typeof(Session.auth) === 'object');
$scope.signin = function() {
$window.location = '/signin/#/' + $routeParams.origin + (Session.auth ? '?email=' + Session.auth.email : '');
};
}]);
我当前的规格尝试:
'user strict';
describe('Testing the errorCtrl controller', function(){
beforeEach(module("rmtg"));
var errorCtrl, scope;
beforeEach(inject(function($controller, $rootScope){
scope = $rootScope;
errorCtrl = $controller("ErrorCtrl", {
$scope: scope
});
}));
it('$scope.status should be set to 404 when location is set to 404', function(){
//set the $location.search values so that the scope is correct
$location.search('status', '404');
expect(scope.status).toBe('404');
});
});
以及当前的错误消息:
当位置设置为404时,测试errorCtrl控制器$scope.status应设置为404失败
预期未定义为“404”。
反对。(/Users/adamremeeting/git/mrp www/app/tests/example.js:20:24)
我也非常感谢使用angular 1.5的tdd上的资源链接,以及我如何正确地模拟和存根
回答后编辑
因此,我根据user2341963建议更新了测试,并尽我所能查看了他的plunker示例,但仍然没有通过测试
当前规范(控制器未从上面更改)
但是现在$controller没有定义,我得到了一个错误。您在测试中得到了
未定义的
,因为您没有在任何地方设置$location
根据您的控制器,必须在初始化控制器之前设置搜索参数。有关完整示例,请参见
describe('testApp', function() {
describe('MainCtrl', function() {
var scope, $location, $controller;
beforeEach(module('myApp'));
beforeEach(inject(function(_$rootScope_, _$controller_, _$location_) {
scope = _$rootScope_.$new();
$location = _$location_;
$controller = _$controller_;
}));
it('should set status to 404', function() {
// Set the status first ...
$location.search('status', '404');
// Then initialise the controller
$controller('MainCtrl', {
$scope: scope,
$location: $location
});
expect(scope.status).toBe('404');
});
});
});
至于资源,到目前为止,我发现这些资源已经足够好了。谢谢您抽出时间。我现在收到一个错误$\u控制器\u未定义。那么,我应该在哪里定义带下划线的版本呢?没关系,这是我的错别字,我不习惯注入器的语法。
describe('testApp', function() {
describe('MainCtrl', function() {
var scope, $location, $controller;
beforeEach(module('myApp'));
beforeEach(inject(function(_$rootScope_, _$controller_, _$location_) {
scope = _$rootScope_.$new();
$location = _$location_;
$controller = _$controller_;
}));
it('should set status to 404', function() {
// Set the status first ...
$location.search('status', '404');
// Then initialise the controller
$controller('MainCtrl', {
$scope: scope,
$location: $location
});
expect(scope.status).toBe('404');
});
});
});