Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了angularjs – 模拟服务以测试控制器大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个Parseservice,我想嘲笑的顺序测试所有的控制器,使用它,我一直在阅读关于茉莉花间谍,但它仍然不清楚我。任何人都可以给我一个例子,如何模拟自定义服务,并在控制器测试中使用它?

现在我有一个控制器使用服务插入一本书:

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.

我做错了什么?

我做错了没有注入mocked服务到beforeEach的控制器:
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,请注明来意。