问题
I want to define tests for my angular app but I don't know how to work with because it's my first time to write tests.
However, it's important for me to define Tests with Jasmine for REST Service in my application. Well, my question is how can I test the GET requests. I was on the jasmine site and have read the sample there. But it was only a test whether the URL is correctly written.
My intention is to check if I get response data back? Is that possible? Also in Angular Doc is written that for $resource is $httpBackend recommended. That's why I'm a bit confused how starting define the tests.
At first my services:
resService
:
testApp.factory('ResService', ['$resource', 'baseUrl',
function ($resource, baseUrl) {
return {
group: $resource(baseUrl + '/api/qr_group/:Id', {
Id: '@Id'
}, {})
}
}]);
CrudService
:
...
getAllGroups: function () {
return ResService.group.query();
},
At last my Controller with the request:
TestCtrl
:
CrudService.getAllGroups().$promise.then(
function (response) { $scope.groups = response; },
function (error) { errMessage(error); }
);
Do anyone has an idea? It would be helpful :)
回答1:
Update for the @downvoter :
I quote from his question : "My intention is to check if I get response data back? Is that possible?" Is testing the backend with angular unit test framework correct ?
My original answer :
You are trying to test your REST API and not the frontend part (angular app.).
This tests must be on backend part, the service in Angular assumes that it receive a good answer, this is why there is $httpBackend, to isolate your service logic from the REST API.
回答2:
What @Moncef said was true. You should not test your REST API with angular. Its the server's job to test that the response from the REST API is correct. However if you still wish to test it.
How do I test an AngularJS service with Jasmine?
回答3:
What you should test is your callbacks for a correct response from the server.
you can use $httpBackend
to mock the server side for that purpose.
You can use:
$httpBackend.expect
$httpBackend.when
The main difference is that the first makes the request mandatory and will make the fail test if it doesn't happen, and will also expect all requests in the order that it(the expect
function) was invoked in the scenario,
while when is more forgiving, and just provides the response when a request is send, and does nothing otherwise.
You can learn more about the differences at the Angular official documentation.
EDIT, full example of expect - respond workflow:
//Group controller spec... success test...
$httpBackend.expect('GET', '/groups').respond('200', {'groups': [{'name': 1}, {'name': 2}]});
//above line tests that the groups controller asks for groups from the backend
$httpBackend.flush();
//above line makes the actual assertion that the request was sent, and sends the mocked response code and data
expect($scope.groups).toEqual([{'name': 1}, {'name': 2}]);
Failure:
//Group controller spec... failure callback test...
$httpBackend.expect('GET', '/groups').respond('500', {'error': 'server error'});
//above line tests that the groups controller asks for groups from the backend
$httpBackend.flush();
//above line makes the actual assertion that the request was sent, and sends the mocked response code and data
expect($scope.groups).toBeUndefined();
//can also spyOne the error handler, and assert that it was triggered...
来源:https://stackoverflow.com/questions/32548886/how-to-define-tests-with-jasmine-for-resource-angularjs