Angular - Jasmine can real XHR be executed?

403 views
Skip to first unread message

Mihai Huluta

unread,
May 2, 2013, 11:54:38 AM5/2/13
to ang...@googlegroups.com
I do not want to have the $http mocked. Main reason is that I am writing some integration tests so I want to have all AJAX requests executed. Currently in my tests none gets triggered.
Any suggestion is more than welcomed.

Michael Bielski

unread,
May 6, 2013, 12:10:20 PM5/6/13
to ang...@googlegroups.com
In tests you DO NOT want to make real XHR requests. Mocking is the best method for testing how your code (not the code on the server, or the server response time, or anything not in your code) is working. That is what testing is all about: testing YOUR code. Simulate your data as best you can. When you come to a point where there is data in a different format or something that hasn't been tested, run it on your tests and see how your code fares. If the tests fail, you need to update your code to make the tests pass (not the other way around.)

Ryan Zec

unread,
May 6, 2013, 12:20:19 PM5/6/13
to ang...@googlegroups.com
Well there are use cases for not mocked every ajax request.  For example, I don't want requests for my templates (which uses $http) to be mocked otherwise I would have to duplicate the templates in the test and keep them in sync.

There is a .passThrough() method on the $httpBackend the should have that request that matches the url/regex make the actually request however right now it is not working for me (on 1.1.4 and 1.0.6).  I have fallen back to using jQuery's $.ajax in sync mode (which works ok for my use case but might not for others).  I am waiting until this issue is either fixed or explained to remove the jQuery calls : https://github.com/angular/angular.js/issues/2512


On Mon, May 6, 2013 at 12:10 PM, Michael Bielski <michael...@yahoo.com> wrote:
In tests you DO NOT want to make real XHR requests. Mocking is the best method for testing how your code (not the code on the server, or the server response time, or anything not in your code) is working. That is what testing is all about: testing YOUR code. Simulate your data as best you can. When you come to a point where there is data in a different format or something that hasn't been tested, run it on your tests and see how your code fares. If the tests fail, you need to update your code to make the tests pass (not the other way around.)

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Jacob Rief

unread,
May 13, 2013, 3:22:42 PM5/13/13
to ang...@googlegroups.com
Why not? Often the server-side code depends on the input from an Ajax request and vice versa. Web application programmers often write the server- AND the client-side code. Its not very DRY to rewrite to server logic in Javascript, just to create a mock server. 

Josh David Miller

unread,
May 14, 2013, 4:39:09 PM5/14/13
to angular
@Jacob - The client shouldn't be testing the server *logic* so there shouldn't be any duplication. A mock server is not a client-side copy of the server. When you test your code, you need to ensure what the client SENDS is right, but what it receives in response is not relevant expect in the context of a test designed specifically for that response; when you need to test how the client reacts to server-side data, you give it fake data to ensure it handles it appropriately. In other words, these should be two separate tests. So in our client-side tests, server-side logic should be irrelevant.

In the case that a developer is responsible for both the front and back-ends, I *strongly* recommend against integrating the tests as it tends to lead to sloppier work. If we code the backend and the frontend to spec rather than to each other, we will find it much easier to maintain and to add new endpoints (e.g. mobile apps) later.

Josh


On Mon, May 13, 2013 at 12:22 PM, Jacob Rief <jacob...@gmail.com> wrote:
Why not? Often the server-side code depends on the input from an Ajax request and vice versa. Web application programmers often write the server- AND the client-side code. Its not very DRY to rewrite to server logic in Javascript, just to create a mock server. 

Anthony Rizzo

unread,
Jan 19, 2014, 8:18:32 AM1/19/14
to ang...@googlegroups.com

So I am interest in this functionality as well, but for different reasons. In my Karma Test Suite, I keep a running log of all the ajax requests I test. My backend test suite runs curl requests of endpoints. So in order to keep track that I am fully testing all of my used endpoints, currently I have to manually I take the information from the Karma Test Suite and plug it into the backend test suite. Ideally at the end of the Karma Test Suite I would like to run an actual PUT request to store this information on the server, and have the backend test suite read from it.
Reply all
Reply to author
Forward
0 new messages