在我的有角JS应用程序中,我有一个mainController,它接受一个userFactory作为参数。
userFactory由一个名为userService的对象组成,该对象又具有一个userDetails对象和一些方法,包括resetUserDetails。 (详见下文)

在mainController中,我有一个logOut函数,该函数调用userFactory.userService.resetUserDetails方法。我想用茉莉花测试此注销功能,但是出现一些错误。我对Jasmine很陌生,所以如果我缺少明显的东西,我深表歉意。

因此,首先在我的Jasmine套件中,创建一个MainControllerSpec来测试我的mainController。

在此规范中,我要注入一个名为userFactory的工厂。我正在尝试spyOn我的resetUserDetails方法,如下所示,但是出现错误:

spyOn(userFactory, 'userService.resetUserDetails');


错误:userService.resetUserDetails()不存在。

我通过在userFactory中(在userService对象之外)创建一个称为test的函数来尝试了此过程,该方法运行良好,因此至少我知道规范中的工厂注入设置良好。
任何帮助,不胜感激。谢谢

MainControllerSpec.js

describe("MainController", function () {
    beforeEach(angular.mock.module('mapModule', 'ngRoute','ngTouch', 'ngAnimate'));
    var scope, userFactory;

    beforeEach(inject(function($rootScope, $controller, _userFactory_){
        scope = $rootScope.$new();
        userFactory = _userFactory_;
        $controller('mainController', {
            $scope: scope
        });
    }));


   describe('The logOut function', function() {
        it('should call the resetUserDetails function of the userFactory.userService object and reset the userDetails object', function() {
            //spyOn takes in a factory and a method of that factory
            spyOn(userFactory, 'userService.resetUserDetails');
            //spyOn(userFactory, 'test'); tried this and it works.
            scope.logOut();
            expect(userFactory.userService.resetUserDetails).toHaveBeenCalled();
        });
    });

});


mainController中的logOut函数

   $scope.logOut = function(){
         userFactory.userService.resetUserDetails();
         //userFactory.test(); //tried this with spyOn in jasmine
    }


userFactory

mapApp.factory('userFactory', function(){

    var userService = {
        /*
         * Initialize a userDetails object.
         */
        userDetails : {
            "userID" : null,
            "facebookUserID" : "",
            "facebookName" : "",
            "facebookProfilePic" : "",
            "userPrivilegeID" : 1,
            "userToken" : "",
            "isLoggedIn" : false
        },
        resetUserDetails : function(){
            /*
             * This method resets the userDetails object.
             */
            this.userDetails = {
                "userID" : null,
                "facebookUserID" : "",
                "facebookName" : "",
                "facebookProfilePic" : "",
                "userPrivilegeID" : 1,
                "userToken" : "",
               "isLoggedIn" : false
            };
        }
    };
    var test = function(){
        /*
        * for testing spyOn in Jasmine
        */
    };
    //return public API so that we can access it in all controllers
    return{
      userService: userService,
      test: test
    };
});

最佳答案

您需要先模拟userFactory,然后才能直接注入它。
单元测试的目标是将文件作为黑盒进行测试,而无需直接测试相关方法的逻辑。

对于他们,您将改为为userFactory编写规格文件。

在这种情况下,您可以执行以下操作:



describe("MainController", function() {

  beforeEach(angular.mock.module('mapModule', 'ngRoute', 'ngTouch', 'ngAnimate'));
  var scope, userFactory;

  // here mock the methods of your factory
  beforeEach(module(function($provide) {
    $provide.value('userFactory', {
      myFirstObject: {
        myFirstMethod: function() {}
      }
    });
  }));

  beforeEach(inject(function($rootScope, $controller, _userFactory_) {
    scope = $rootScope.$new();
    userFactory = _userFactory_;
    $controller('mainController', {
      $scope: scope
    });
  }));


  describe('The logOut function', function() {
    it('should call the resetUserDetails function of the userFactory.userService object and reset the userDetails object', function() {
      //here spy on the method and return what you would like to return in this test
      // or if you don't need to manage the return, as it seems you don't, just use callThrough
      spyOn(userFactory.myFirstObject, 'myFirstMethod').and.callThrough();
      scope.logOut();
      expect(userFactory.myFirstObject.myFirstMethod).toHaveBeenCalled();
    });
  });

});

关于javascript - 将Jasmine spyOn与工厂中定义的对象方法一起使用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46632773/

10-09 23:39