问题是
在您的生产代码中
var dataLoadedPromise = $http.get('myData.xml', {transformResponse: transformResponse});
你期望服务器返回的是XML
数据而不是 json,所以当你模拟 HTTP 时,你应该使用 XML 而不是 Json。
这是工作代码:
describe("controller: MyController", function () {
beforeEach(function () {
module("myApp");
});
var dataFactory;
beforeEach(inject(function ($injector, $controller, $rootScope, $location, $httpBackend) {
this.$location = $location;
this.$httpBackend = $httpBackend;
this.scope = $rootScope.$new();
dataFactory = $injector.get('dataFactory');
this.$httpBackend.expectGET('myData.xml').respond(
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?><myData><persons><person><firstName>Pera</firstName><lastName>Peric</lastName><email>perica@gmail.com</email><score>22</score></person></persons></myData>");
$controller('MyController', {
$scope: this.scope,
$location: $location,
myData: dataFactory
});
}));
afterEach(function () {
this.$httpBackend.verifyNoOutstandingRequest();
this.$httpBackend.verifyNoOutstandingExpectation();
});
describe("successfully parsed persons", function () {
it("should have 2 persons", function () {
dataFactory.getData();
this.$httpBackend.flush();
});
});
});
Updated Demo