Jasmine + AngularJS:如何为工厂模拟JSON响应?

时间:2014-08-06 16:22:21

标签: angularjs

我有一个控制器:

app.controller('MensDetailCtrl', ['$scope', '$resource', '$controller', 'Product', function($scope, $resource, $controller, Product) {

  $scope.init = function(id)
  {
    $scope.product = Product.get({productId: id}, function(data) {
      $scope.products_colors = data.products_colors.filter(function(product_color) {
        return product_color.mens == true;
      });
      $scope.setSelectedColor(data);      
    });

  }

 ....

我的工厂:

app.factory('Product', ['$resource', function($resource) {
  return $resource("/api/products/:productId", {}, {
    query: {method: 'GET', isArray: true},
  });
}]);

使用注意Product.get(...)。在我的Jasmine测试中:

describe('init(id)', function() {
  //how do I stub the Product.get to return an array of objects [{name: "Soda", price: "11.22"}]
});

如何将Product.get存根以返回对象数组[{name:“Soda”,price:“11.22”}]?

0 个答案:

没有答案