I’ve been doing a lot of testing lately in AngularJS, as I’m sure you can tell from my many posts as of late.
One thing I’m always curious about is whether or not I’m doing things correctly. Testing always helps reinforce this, as does publishing blogs and getting feedback from my peers.
Problem
Many times I’ll have my AngularJS service fire off an HTTP post request to the server for a message. I can’t even begin to tell you how much I sometimes butcher my POST request data.
I wrote a test to verify my post data was correct for the following function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
Pretty simple, nothing too fancy.
I want to test this bad boy and make sure its passing the correct post data parameters. Luckily for us, AngularJS gives us our friendly $httpBackend
tool to do things like this:
1 2 |
|
One thing to note, is the function for [data]
is a version of the POST data object after its been run through something like JSON.stringify
.
A full test looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
Going forward, there should be no excuses as to why my HTTP post requests fail due to parameters being passed or set incorrectly.
I hope this helps any others looking to test their post data parameters.