我正在尝试编写一个单元测试来测试一个执行http.get的工厂,然后测试范围绑定。
在我的控制器内调用工厂。
这是一个显示我的http.get:http://plnkr.co/edit/VqUSeTiEj3MP37tAXKad?p=preview
的傻瓜控制:
app.controller('MainCtrl', function($scope, $http, factoryGetJSONFile) {
$scope.name = 'World';
factoryGetJSONFile.getMyData(function(data) {
$scope.Addresses = data.Addresses.AddressList;
$scope.People = data.Names.People;
$scope.Country = data.Country;
});
});
测试:
describe('with httpBackend', function () {
var app;
beforeEach(function () {
app = angular.mock.module('plunker')
});
describe('MyCtrl', function () {
var scope, ctrl, theService, httpMock;
beforeEach(inject(function ($controller, $rootScope, factoryGetJSONFile, $httpBackend) {
httpMock = $httpBackend;
scope = $rootScope.$new();
ctrl = $controller('MyCtrl', {
$scope: scope,
factoryGetJSONFile: factoryGetJSONFile,
$httpBackend: httpMock
});
}));
it("should make a GET call to data.json", function () {
console.log("********** SERVICE ***********");
httpMock.expectGET("data.json?").respond(data);
console.log(data.Addresses);
console.log(data.Names);
console.log(data.Country);
//expect(factoryGetJSONFile.getMyData()).toBeDefined();
httpMock.flush();
});
})
});
http.get的测试似乎没问题,但是当我尝试记录响应(数据)时,会发生错误。
更新:
当我尝试通过以下方式记录电话时:
console.log(httpMock.expectGET("data.json?").respond(data));
显示未定义。