使用Jasmine spyOn和工厂

时间:2017-10-08 15:25:01

标签: javascript angularjs jasmine spyon

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

在mainController中,我有一个调用userFactory.userService.resetUserDetails方法的logOut函数。我想用jasmine测试这个logOut函数,但是我遇到了一些错误。我是Jasmine的新手,如果它显而易见的话,我很抱歉。

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

在这个规范中,我正在注入一个名为userFactory的工厂。我正在尝试使用我的resetUserDetails方法监视如下错误:

spyOn(userFactory, 'userService.resetUserDetails');

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

我通过在userFactory中创建一个名为test的函数(在userService对象之外)尝试了这个过程,并且它运行良好,所以至少我知道规范中的工厂注入设置正常。
任何帮助非常感谢。谢谢

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
    };
});

1 个答案:

答案 0 :(得分:1)

您需要先模拟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();
    });
  });

});