Reputation: 6071
I have a problem with jasmine and angular. I have to test my factories. I have made simplidfied project on plunker. Here is demo app: http://plnkr.co/edit/Agq4xz9NmYeEDWoJguxt Here is demo test: http://plnkr.co/edit/ALVKdXO00IEDaKIjMY6u The first problem is how to make spec runner working without any specification to test.
When you run demo test plunker you will see error:
TypeError: myDataRaw is undefined in http: //run.plnkr.co/Kqz4tGMsdrxoFNKO/app.js (line 39)
Somebody can help?
Thanks
Upvotes: 1
Views: 3634
Reputation: 54504
The problem is
In your production code
var dataLoadedPromise = $http.get('myData.xml', {transformResponse: transformResponse});
you expect the return from the server is XML
data not json, so when you mock the HTTP, you should use XML instead of Json.
Here is the working code:
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>[email protected]</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();
});
});
});
Upvotes: 2