你怎么嘲笑一个angularjs $资源工厂 [英] How do you mock an angularjs $resource factory

查看:131
本文介绍了你怎么嘲笑一个angularjs $资源工厂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个资源工厂

angular.module('mean.clusters').factory('Clusters', ['$resource',
  function($resource) {
    return $resource('clusters/:clusterId/:action', {
        clusterId: '@_id'
    }, {
        update: {method: 'PUT'},
        status: {method: 'GET', params: {action:'status'}}
    });
}]);

和控制器

angular.module('mean.clusters').controller('ClustersController', ['$scope',
  '$location', 'Clusters',
    function ($scope, $location, Clusters) {
        $scope.create = function () {
            var cluster = new Clusters();

            cluster.$save(function (response) {
                $location.path('clusters/' + response._id);
            });
        };

        $scope.update = function () {
            var cluster = $scope.cluster;

            cluster.$update(function () {
                $location.path('clusters/' + cluster._id);
            });
        };


        $scope.find = function () {
            Clusters.query(function (clusters) {
                $scope.clusters = clusters;

            });
        };
}]);

我写我的单元测试,我发现每一个例子是使用某种形式的 $ httpBackend.expect 的嘲笑来自服务器的响应,而我能做的,只是罚款。

I am writing my unit tests and every example I find is using some form of $httpBackend.expect to mock the response from the server, and I can do that just fine.

我的问题是,当单元测试我控制器的功能,我想嘲笑的对象集群。如果我用 $ httpBackend.expect ,我在我厂引进的错误在我的控制器每个单位会导致测试失败。

My problems is, when unit testing my controller functions I would like to mock the Clusters object. If I'm using $httpBackend.expect, and I introduce a bug in my factory every unit test in my controller will fail.

我想有我的测试 $ scope.create 测试仅 $ scope.create 键,也没有我厂code。

I would like to have my test of $scope.create test only $scope.create and not also my factory code.

我已经尝试添加一个供应商的 beforeEach(模块('平均',函数($提供){我的测试的一部分,但我似乎无法得到它正确的。

I've tried adding a provider in the beforeEach(module('mean', function ($provide) { part of my tests but I cant seem to get it right.

我也试过

clusterSpy = function (properties){
    for(var k in properties)
        this[k]=properties[k];
};

clusterSpy.$save = jasmine.createSpy().and.callFake(function (cb) {
    cb({_id: '1'});
});

和制定集群= clusterSpy; 之前(注但在创建功能,间谍丢失以

and setting Clusters = clusterSpy; in the before(inject but in the create function, the spy gets lost with

错误:预期间谍,却得到了功能

Error: Expected a spy, but got Function.

我已经能够得到一个间谍的对象为的集群。$更新呼叫类型的工作,但随后在 VAR集群= new失败集群(); 以不是一个函数错误

I have been able to get a spy object to work for the cluster.$update type calls but then it fails at var cluster = new Clusters(); with a 'not a function' error.

我可以创建为 VAR集群=新集群()工作的函数; 但后来失败了集群$更新呼叫类型。

I can create a function that works for var cluster = new Clusters(); but then fails for the cluster.$update type calls.

我可能在这里混条件,但,有没有嘲笑集群与功能的间谍或正确的方法是有一个很好的理由,只是 $ httpBackend.expect

I'm probably mixing terms here but, is there a proper way to mock Clusters with spies on the functions or is there a good reason to just go with $httpBackend.expect?

推荐答案

看起来像我很接近了几次,但我认为我有现在想通了。

Looks like I was close a few times but I think I have it figured out now.

该解决方案是上面的'我也试过的一部分,但我没有从函数返回的谍对象。

The solution was the 'I also tried' part above but I was not returning the spy object from the function.

这个工作,它可以放置在任何的 beforeEach(模块( beforeEach(注部分

This works, it can be placed in either the beforeEach(module( or beforeEach(inject sections

第1步:用你要测试并将其分配给一个变​​量,对你的测试访问的任何功能创建间谍对象

Step 1: create the spy object with any functions you want to test and assign it to a variable that's accessible to your tests.

第二步:使返回间谍对象的函数

Step 2: make a function that returns the spy object.

第3步:间谍对象的属性复制到新的功能

Step 3: copy the properties of the spy object to the new function.

clusterSpy = jasmine.createSpyObj('Clusters', ['$save', 'update', 'status']);

clusterSpyFunc = function () {
    return clusterSpy
};

for(var k in clusterSpy){
    clusterSpyFunc[k]=clusterSpy[k];
}

步骤4:将它添加到 beforeEach(注部分$控制器

Step 4: add it to the $controller in the beforeEach(inject section.

ClustersController = $controller('ClustersController', {
    $scope: scope,
    Clusters: clusterSpyFunc
});

你的测试里面可以使用静止功能添加到方法

inside your tests you can still add functionality to the methods using

clusterSpy.$save.and.callFake(function (cb) {
    cb({_id: '1'});
});

然后检查间谍值

expect(clusterSpy.$save).toHaveBeenCalled();

这解决了新的集群这两个问题() Clusters.query 不是一个函数。现在我可以进行单元测试我控制器出来的资源工厂的依赖。

This solves both problems of new Clusters() and Clusters.query not being a function. And now I can unit test my controller with out a dependency on the resource factory.

这篇关于你怎么嘲笑一个angularjs $资源工厂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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