大佬教程收集整理的这篇文章主要介绍了angularjs – 模拟服务以测试控制器,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
BookCrossingApp.controller('AddBookCtrl',function ($scope,Dataservice,$LOCATIOn) { $scope.registerNewBook = function (book) { Dataservice.registerBook(book,function (isResult,result) { $scope.$apply(function () { $scope.registerResult = isResult ? "success" : result; }); if (isResult) { //$scope.registerResult = "success"; $LOCATIOn.path('/main'); } else { $scope.registerResult = "Fail!"; //$LOCATIOn.path('/'); } }); }; });
服务是这样的:
angular.module('Dataservices',[]) /** * Parse service * Use Parse.com as a BACk-end for the application. */ .factory('Parseservice',function () { var Parseservice = { name: "Parse",registerBook: function registerBook(bookk,callBACk) { var book = new Book(); book.set("title",bookk.titlE); book.set("description",bookk.Description); book.set("registrationId",bookk.RegistrationId); var newAcl = new Parse.ACL(Parse.User.current()); newAcl.setPublicReadAccess(true); book.setACL(newAcl); book.save(null,{ success: function (book) { // The object was saved successfully. callBACk(true,null); },error: function (book,error) { // The save Failed. // error is a Parse.Error with an error code and description. callBACk(false,error); } }); } }; return Parseservice; });
到目前为止我的测试看起来像这样:
describe('Controller: AddBookCtrl',function() { // // load the controller's module beforeEach(module('BookCrossingApp')); var AddBookCtrl,scope,book; // Initialize the controller and a mock scope beforeEach(inject(function($controller,$rootScopE) { scope = $rootScope; book = {title: "footitle13"}; AddBookCtrl = $controller('AddBookCtrl',{ $scope: scope }); })); it('should call Parse service method',function () { //We need to get the injector from angular var $injector = angular.injector([ 'Dataservices' ]); //We get the service from the injector that we have called var mockservice = $injector.get( 'Parseservice' ); mockservice.registerBook = jasmine.createSpy("registerBook"); scope.registerNewBook(book); //With this call we SPY the method registerBook of our mockservice //we have to make sure that the register book have been called after the call of our Controller expect(mockservice.registerBook).toHaveBeenCalled(); }); it('Dummy test',function () { expect(true).toBe(true); }); });
现在测试失败:
Expected spy registerBook to have been called. Error: Expected spy registerBook to have been called.
我做错了什么?
describe('Controller: AddBookCtrl',function() { var scope; var Parseservicemock; var AddBookCtrl; // load the controller's module beforeEach(module('BookCrossingApp')); // define the mock Parse service beforeEach(function() { Parseservicemock = { registerBook: function(book) {},getBookRegistrationId: function() {} }; }); // inject the required services and instantiate the controller beforeEach(inject(function($rootScope,$controller) { scope = $rootScope.$new(); AddBookCtrl = $controller('AddBookCtrl',{ $scope: scope,Dataservice: Parseservicemock }); })); it('should call registerBook Parse service method',function () { var book = {title: "footitle"} spyOn(Parseservicemock,'registerBook').andCallThrough(); //spyOn(Parseservicemock,'getBookRegistrationId').andCallThrough(); scope.registerNewBook(book); expect(Parseservicemock.registerBook).toHaveBeenCalled(); //expect(Parseservicemock.getBookRegistrationId).toHaveBeenCalled(); }); });
以上是大佬教程为你收集整理的angularjs – 模拟服务以测试控制器全部内容,希望文章能够帮你解决angularjs – 模拟服务以测试控制器所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。