这很好用:
spyOn($state, 'go');
但是当我执行下面的行时,它不起作用并发出此错误:
'期待的间谍去了[' stateName' ]但确实如此 从未打过电话'
expect($state.go).toHaveBeenCalledWith('stateName');
describe('Home Controller Test Suite', function() {
var HomeCtrl,$rootScope, $controller, $scope, $http, $state,factory;
var baseURL = "http://localhost:8000/";
beforeEach(module("ui.router"));
beforeEach(module("rbControllers"));
beforeEach(module("ui.bootstrap"));
beforeEach(module("toggle-switch"));
beforeEach(module("ngDraggable"));
beforeEach(module("angular-loading-bar"));
beforeEach(module("ngAnimate"));
beforeEach(module("kendo.directives"));
beforeEach(module("rbApp"));
beforeEach(inject(function(_$rootScope_ ,_$controller_,_$httpBackend_, _$state_){
$rootScope = _$rootScope_;
$scope = $rootScope.$new();
$controller = _$controller_;
httpBackend = _$httpBackend_;
$state = _$state_;
HomeCtrl = $controller('HomeCtrl', {'$scope': $scope, '$state': $state, 'baseURL' : baseURL, 'roobrickUiService': factory});
}));
it("Test: HomeCtrl exists or not", function() {
expect(HomeCtrl).toBeDefined();
});
it("Test:xxx run", function() {
spyOn($state, 'go');
expect($state.go).toHaveBeenCalledWith('stateName');
});
});