Mock XHR request in testing to use a JSON file

637 views
Skip to first unread message

jks...@gmail.com

unread,
Jun 5, 2012, 7:20:47 AM6/5/12
to ang...@googlegroups.com
Hey guys, when I am unit testing Angular code using $httpBackend, how can I respond with a json file instead of hard coding it in the test file?

Snippet:

$httpBackend.expectGET('../test/unit/product/products.json').
          respond([{short_code: 'Bulk'}, {short_code: 'Outsourced'}]);

I am only just getting started with programming, so let me know if this isn't the right way to do it.  Basically, I am looking to keep the json file separate so it is easy for me to modify that file if the backend data changes without changing the test case.

Thanks!

Misko Hevery

unread,
Jun 6, 2012, 8:45:24 PM6/6/12
to ang...@googlegroups.com
expectations are synchronous and so the data must be defined inline. There is no way around it.

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/delmb5wDqacJ.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/angular?hl=en.

Renan T. Fernandes

unread,
Jun 6, 2012, 9:13:56 PM6/6/12
to ang...@googlegroups.com
in tests include a js file with the json data in a variable and use it on respond

2012/6/6 Misko Hevery <mi...@hevery.com>



--
Renan Fernandes(aka "ShadowBelmolve")

Jaap Karan Singh

unread,
Jun 6, 2012, 9:16:21 PM6/6/12
to ang...@googlegroups.com
Yeh, good idea.

Thanks guys.

vshy...@twinfield.com

unread,
Dec 14, 2012, 7:46:50 AM12/14/12
to ang...@googlegroups.com, mi...@hevery.com
Is that still same situation? 

I have .json files automatically generated as a reference responses from backend and would like to use them in tests. Is there any way how to do that?

Четвер, 7 червня 2012 р. 03:45:24 UTC+3 користувач Misko Hevery написав:
Reply all
Reply to author
Forward
0 new messages