我正在尝试提供一个虚拟服务来获取Angular中的数据,我正在使用ngMockE2E,而我的Mock代码如下所示:
(function () {
"use strict"
var app = angular
.module("productResourceMock", ["ngMockE2E"]);
app.run(function ($httpBackend) {
var products = [
{
"productId": 1,
"productName": "mobile1",
"productCode": "heh4",
"releaseDate": "May 21, 2013",
"description": "very nice mobile",
"cost": 200,
"price": 300,
"category": "Electronics",
"tags": ["mobile", "electronic"],
"imageUrl": "images/img1.jpg"
},
{
"productId": 2,
"productName": "mobile2",
"productCode": "heh4",
"releaseDate": "May 21, 2012",
"description": "not a nice mobile",
"cost": 100,
"price": 500,
"category": "Electronics",
"tags": ["mobile", "Electronic"],
"imageUrl": "images/img2.jpg"
}];
var productUrl = "/api/products";
$httpBackend.whenGet(productUrl).respond(products);
});
}());
我已经定义了 Controller ,并且在其中具有以下代码:
(function () {
"use strict"
angular
.module("productManagement")
.controller("ProductListCtrl",
["productResource",
ProductListCtrl]);
function ProductListCtrl(productResource) {
var vm = this;
productResource.query(function(data){
vm.products = data;
});
}
}());
对于发送REST请求的服务,我有以下代码:
(function () {
"use strict"
angular
.module("common.services")
.factory("productResource",
["$resource", productResource]);
function productResource($resource) {
return $resource("/api/products/:productId");
}
}());
仍然出现此错误:未捕获的TypeError:$ httpBackend.whenGet不是一个函数。
感谢您的帮助,或需要任何澄清,请让我知道。
最佳答案
答案很简单:将whenGet
替换为whenGET
小心地将http动词全部大写。
参见 ngMock documentation
关于angularjs - $ httpBackend .whenGet不是Angular中的函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32933725/