I just started writing my first unit tests in AngularJS via Jasmine.
Somehow I still do not understand why I should mock the $httpBackend. To make clear what's still unclear to me I will write down a small example:
Imagine I have a service (myService) that's getting data from an URL:
function getData() {
return $http.get("http://example.com/data")
.then(function (response) {
return response.data;
});
}
Let's assume that a GET call to the URL "http://example.com/data" returns following data:
{
firstname: "John",
lastname: "Doe"
}
The corresponding test would look like this:
describe("Service: myService", function () {
beforeEach(module("myApp"));
var myService, $httpBackend;
beforeEach(inject(function (_myService_, _$httpBackend_) {
myService = _myService_;
$httpBackend = _$httpBackend_;
}));
afterEach(function () {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
it("should get data", function() {
var mockData = {datakey: "datavalue"};
$httpBackend.whenGET("http://example.com/data").respond(mockData);
var promise = myService.getData();
$httpBackend.flush();
promise.then(function(response){
expect(response).toEqual(mockData)
});
})
});
Unless I am mistaken, the test should pass, although the mocked data is not equal to the real data. The test would always pass, no matter how I set the mocked Data, because the service function would always be redirected to what's set in $httpBackend.whenGET("http://example.com/data").respond(mockData);.
I thought the purpose of such a test is to check if the returned data from a GET call [in this case myService.getData()] is REALLY the expected data and not some random mocked data. So whats the actual point of mocking data instead of checking if myService.getData returns the real data {firstname: "John", lastname: "Doe"}?
I'm well aware that I could also set the mocked Data to {firstname: "John", lastname: "Doe"}, but when the real data from the URL would be dynamic, the mocked Data and the real data wouldn't be equal again.
Thank you in advance!
Aucun commentaire:
Enregistrer un commentaire