1

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 :)

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

exemple on testing your backend code ??? use, the tools offered by your backend. you do not have to test this on angular app side as I explained.
0

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?

Comments

0

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...

9 Comments

Done, please see EDIT
Thanks Oleg.. I will try your example in my Angular App and let you know as soon as possible.
@OlegTikhonov I quote from his question : "My intention is to check if I get response data back? Is that possible?" Here you are mocking the backend not checking if it's getting back. So, you are not answering his question. Is testing the backend with angular unit test framework correct ?
I think you have misunderstood the question, he is clearly asking about what is commonly known as mocking, the author even attached a reference to $httpBackend.
@MoncefHassein-bey My intention is to test the correct callback. That means I want to test the request whether the service is correctly defined.. I'm new in writing tests sorry..
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.