在我的控制器中,我们定义了以下两种方法 -
function goToHome() {
$state.go('app.home', {newReleaseIds: vm.newReleaseIds});
}
function createAnotherFuelRelease() {
// GA -- start creating another fuel release
$analytics.eventTrack('Start creating another fuel release', {category: 'Iron', label: moment().format('MMMM Do YYYY, h:mm:ss a')});
$state.go('app.create-iron', {selectedLocation: vm.selectedLocation, fuelReleaseNumber: vm.fuelReleaseNumber + 1, newReleaseIds: vm.newReleaseIds, pricing: $state.params.pricing});
}
从控制器调用这两个方法 -
vm.createAnotherFuelRelease = createAnotherFuelRelease;
vm.goToHome = goToHome;
现在我想从spec.js文件中测试这些方法 -
它('应检查goToHome()',function(){
// spyOn($state, 'go');
// $scope.inviteMembers(1);
// expect($state.go).toHaveBeenCalledWith('invite', {deptId: 1});
var spy = sinon.spy();
scope.vm.goToHome = {goToHome : spy};
scope.$digest();
expect($state.go).toHaveBeenCalledWith('app.home', {newReleaseIds: 1});
// expect(spy.calledOnce).toEqual(false);
// $compiledElement.find('.nv-button.test-gotToHome').trigger('click');
// expect(spy.calledOnce).toEqual(true);
// spy.reset();
});
但它不起作用。如果有人知道这些,请告诉我。
答案 0 :(得分:0)
在这里你可以找到一个例子。它适用于$ location服务,但想法是一样的。 用$ state替换$ location并确保加载其模块。
'use strict';
angular.module('test', []);
angular.module('test').controller('ctrl', function ctrlFactory($location) {
var vm = this;
vm.greet = function() {
$location.path('foo');
};
});
beforeEach(module('test'));
it('', inject(function($injector) {
// Given
var $controller = $injector.get('$controller');
var $location = $injector.get('$location');
var scope = $injector.get('$rootScope').$new();
spyOn($location, 'path');
var ctrl = $controller('ctrl', { $scope: scope });
// When
scope.$apply();
ctrl.greet();
//Then
expect($location.path).toHaveBeenCalled();
}));