这是我的控制器:
angular.module('someapp', [])
.controller('TestCtrl', function($scope, dataservice, $stateParams) {
$scope.messageid = parseInt($stateParams.messageid);
});
规范文件:
describe('TestCtrl', function() {
beforeEach(module('someapp'));
var $controller;
$stateParameters = { messageid: 100 };
beforeEach(inject(function(_$controller_){
$controller = _$controller_;
}));
it('should set value of messageid', function() {
var controller = $controller('TestCtrl', {
$stateParams: $stateParameters
});
expect($scope.messageid).toEqual($stateParameters.messageid);
});
});
但是它给出了错误:TypeError: $controller is not a function
任何人都可以帮我解决这个问题吗?
答案 0 :(得分:0)
angular.module('someapp', [])
.controller('TestCtrl', ['$scope', '$stateParams', function($scope, $stateParams) {
$scope.messageid = parseInt($stateParams.messageid);
}]);
describe('TestCtrl', function() {
beforeEach(module('someapp'));
var $controller;
$stateParameters = { messageid: 100 };
beforeEach(inject(function(_$controller_){
$controller = _$controller_;
}));
it('should set value of messageid', function() {
var controller = $controller('TestCtrl', {
$stateParams: $stateParameters;
});
expect($scope.messageid).toEqual($stateParameters.messageid);
});
});
答案 1 :(得分:0)
describe('TestCtrl', function() {
var $scope, dataservice;
beforeEach(module('someapp'));
module(function ($provide) {
// Mock dataservice
$provide.service('dataservice', function () {
// Assuming you have a getData function in dataservice
this.getData = jasmine.createSpy('getData');
});
});
//var $controller;
var $stateParameters = { messageid: 100 };
beforeEach(inject(function(_$controller_, _$rootScope_, _dataservice_){
$scope = _$rootScope_.$new();
dataservice = _dataservice_
$controller('TestCtrl', {
$scope : $scope,
dataservice: dataservice,
$stateParams: $stateParameters
});
}));
it('should set value of messageid', function() {
expect($scope.messageid).toEqual($stateParameters.messageid);
});
});