用 Jasmine 测试 AngularJS 工厂函数 [英] Test AngularJS factory function with Jasmine

查看:16
本文介绍了用 Jasmine 测试 AngularJS 工厂函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此很陌生(angularjs、jasmine、testacular)并且我有这段代码(我简化了一点,只留下重要的部分):

I am very new to this (angularjs, jasmine, testacular) and I have this code (I simplified it a bit, leaving only what matters):

//my_module.js
angular.module('my_module', ['my_data'])
.config([...]);

.controller('my_controller', ['$scope', 'my_data',
    function($scope, my_data) {
        $scope.my_function = function() {
            return my_data.my_factory.save().then(function () {
                console.log('saved');
            },
            function() {                             
                console.log('Error');
            }); 
        }
    }
)

//my_data.js
angular.module('my_data', [])
.factory('my_factory', ['$q', '$rootScope',
    function($q, $rootScope) {
        var my_factory= function(my_data) {
            angular.extend(this, my_data);
        }
        my_factory.prototype.save = function() {
            var deferred = $q.defer();
            setTimeout(function() {
                deferred.resolve();
            $rootScope.$apply();
            }, 1000);

            return deferred.promise;
        }
        return my_factory;
    }
])

所以,我想做的是测试 my_data.my_factory.save 在 my_module.my_controller.my_function 被触发时是否被调用.

So, what I want to do is to test if my_data.my_factory.save is called when the my_module.my_controller.my_function is fired.

//my_test.js
describe('testing my_controller.my_function', function () {
    beforeEach(module('my_module'));

    var $rootScope, $controller;
    beforeEach(inject(function(_$rootScope_, _$controller_) {
        $rootScope = _$rootScope_;
        $controller = _$controller_;
    }));

    scope = $rootScope.$new();

    it('should call the save function', function() {
        scope.my_function();
        expect(my_data.save).toHaveBeenCalled();
    });
}

我需要一点帮助.

推荐答案

你离你需要的并不远.首先,由于您需要 my_data 作为 my_module 依赖项,因此您不需要将 my_module 注入控制器,只需将工厂 (my_factory);

You're not too far from what you need. First off, as you require my_data as my_module dependency, you don't need to inject my_module to the controller, just the factory (my_factory);

其次,您要使用 ngMock.文档不是很完整,但给出了很好的见解.更多这里和一个此处的示例(查找 test/unit/controllers).

Secondly, you want to make use of ngMock. The docs are not very complete, but give a good insight. More here and a example here (look for test/unit/controllers).

基本上,您要做的是模拟该服务,以便确保它已被调用.要实现它,请将 $provide 注入您的 angular.mock.module 调用并提供模拟的 my_factory 服务.实现它的最佳方法是这样的:

Basically, what you want to do is to mock the service so you can be assured it has been called. To achieve it, inject $provide to your angular.mock.module call and provide a mocked my_factory service. The best way to achieve it is something like this:

describe('testing my_controller.my_function', function () {
  var mockedFactory, $rootScope, $controller;

  beforeEach(module('my_module', function($provide) {
    mockedFactory = {
      save: jasmine.createSpy()
    };

    $provide.value('my_factory', mockedFactory);
  }));

  beforeEach(inject(function(_$rootScope_, _$controller_) {
    $rootScope = _$rootScope_;
    $controller = _$controller_;
  }));

  scope = $rootScope.$new();

  it('should call the save function', function() {
    scope.my_function();
    expect(mockedFactory.save).toHaveBeenCalled();
  });
}

这样您将覆盖 my_factory 依赖项.

This way you'll override my_factory dependency.

这篇关于用 Jasmine 测试 AngularJS 工厂函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆