【发布时间】:2014-04-12 15:52:59
【问题描述】:
我有这个简单的控制器,UserService 是一个返回 JSON 的服务
"use strict";
angular.module("controllers").controller('profileCtrl', ["$scope", "UserService",
function ($scope, UserService) {
$scope.current_user = UserService.details(0);
}
]);
我无法进行测试。不过这是我的尝试
'use strict';
describe('profileCtrl', function () {
var scope, ctrl;
beforeEach(angular.mock.module('controllers'), function($provide){
$provide.value("UserService", {
details: function(num) { return "sdfsdf"; }
});
});
it('should have a LoginCtrl controller', function() {
expect(controllers.profileCtrl).toBeDefined();
});
beforeEach(angular.mock.inject(function($rootScope, $controller){
scope = $rootScope.$new();
$controller('profileCtrl', {$scope: scope});
}));
it('should fetch list of users', function(){
expect(controllers.scope.current_user.length).toBe(6);
expect(controllers.scope.current_user).toBe('sdfsdf');
});
});
【问题讨论】:
-
你需要解释你的问题到底是什么。 “它不起作用”还不够好。
-
只是我不了解测试的结构,我尝试了很多示例但没有得到它
标签: angularjs unit-testing controller karma-runner karma-jasmine