Angularjs 如何使用Jasmine测试回调中执行的操作
我有两个控制器和一个服务。在第一个控制器中,我订阅了一个事件来做一些事情。第二个控制器执行一些操作,完成后广播事件。请参见下面的示例,超时仅用于模拟长时间运行的操作。我想用Jasmine 2.0测试hasLoaded是否设置为true,请告知Angularjs 如何使用Jasmine测试回调中执行的操作,angularjs,jasmine,angular-services,Angularjs,Jasmine,Angular Services,我有两个控制器和一个服务。在第一个控制器中,我订阅了一个事件来做一些事情。第二个控制器执行一些操作,完成后广播事件。请参见下面的示例,超时仅用于模拟长时间运行的操作。我想用Jasmine 2.0测试hasLoaded是否设置为true,请告知 var myApp=angular.module('myApp',[]); myApp.controller('MyCtrl1',['$scope','myService',function($scope,myService){ $scope.haslo
var myApp=angular.module('myApp',[]);
myApp.controller('MyCtrl1',['$scope','myService',function($scope,myService){
$scope.hasload=false;
$scope.fileName='';
myService.onload($scope,function(e,data){
//我想测试以下两行代码,实际上这里的代码要复杂得多
$scope.fileName=data.fileName;
$scope.hasload=true;
});
}]);
控制器('MyCtrl2'、['$rootScope'、'$scope'、'$timeout'、'myService',函数($rootScope、$scope、$timeout、myService){
$scope.isLoading=false;
$scope.title='单击我加载';
$scope.load=函数(){
$scope.isLoading=true;
$scope.title='正在加载,请稍候…';
$timeout(函数(){
$rootScope.$emit('loaded',{fileName:'test.txt'});
}, 1000);
};
myService.onload($scope,function()){
$scope.hasload=true;
});
}]);
myApp.service('myService',['$rootScope',function($rootScope){
this.onload=函数(作用域、回调){
var handler=$rootScope.$on('loaded',callback);
作用域:$on(“$destroy”,处理程序);
};
}]);代码>
{{fileName}}已加载!!!
您真的应该分别测试每个部件(控制器和服务)。在您的情况下,正确设置hasLoaded的控制器的测试实际上只需要测试您向服务的注册是否正确,以及回调是否符合您的预期:
it("should register with the service and do the right thing when the callback is executed", inject(function ($controller, $rootScope, myService) {
var $scope = $rootScope.$new();
spyOn(myService, 'onLoaded').and.callThrough();
var ctrl = $controller('MyCtrl1', {$scope: $scope, myService: myService});
$scope.$apply();
//verify that the controller registers its scope with the service
expect(myService.onLoaded).toHaveBeenCalledWith($scope, jasmine.any(Function));
//now call the callback that was registered to see if it sets the property correctly
var mockData = {
fileName: 'some file name'
};
myService.onLoaded.calls.argsFor(0)[1]('loaded', mockData);
expect($scope.hasLoaded).toBeTruthy();
expect($scope.fileName).toBe("some file name");
}));
然后分别为您的服务和其他控制器编写测试。您确实应该分别测试每个部分(控制器和服务)。在您的情况下,正确设置hasLoaded的控制器的测试实际上只需要测试您向服务的注册是否正确,以及回调是否符合您的预期:
it("should register with the service and do the right thing when the callback is executed", inject(function ($controller, $rootScope, myService) {
var $scope = $rootScope.$new();
spyOn(myService, 'onLoaded').and.callThrough();
var ctrl = $controller('MyCtrl1', {$scope: $scope, myService: myService});
$scope.$apply();
//verify that the controller registers its scope with the service
expect(myService.onLoaded).toHaveBeenCalledWith($scope, jasmine.any(Function));
//now call the callback that was registered to see if it sets the property correctly
var mockData = {
fileName: 'some file name'
};
myService.onLoaded.calls.argsFor(0)[1]('loaded', mockData);
expect($scope.hasLoaded).toBeTruthy();
expect($scope.fileName).toBe("some file name");
}));
然后分别为您的服务和其他控制器编写测试。谢谢,这就是我需要的。您能告诉我为什么我们需要$scope。$apply();打电话?在你的情况下你真的不知道。我只是习惯于这样做,因为最近我一直在用模拟承诺进行测试,这些承诺需要$apply()才能在测试中得到解决。谢谢,这就是我需要的。您能告诉我为什么我们需要$scope。$apply();打电话?在你的情况下你真的不知道。我只是习惯于这样做,因为最近我一直在用需要$apply()才能在测试中解决的模拟承诺进行测试。