HTTP Mocking for ember-testing

If you happen to be using https://github.com/trek/ember-testing-httpRespond it is now included with ember app kit https://github.com/stefanpenner/ember-app-kit/pull/382

1 Like

These helpers are a thing of beauty! Thanks for the work youā€™ve done :smile:

When talking sinon and Ember.js combination I recommend you have a look at the sinon/Ember.js runloop note at ember.js - How do I test Ember.run.later with Sinon? - Stack Overflow

@trek I tried to use this just now, but it causes all my other tests to break (Iā€™m including it globally in my testsā€¦) so I might have to just go back to using mockjax for now.

I kinda really liked the idea that this library delivers, though: individual request-specific responses is a really cool pattern. Iā€™m also using Ember Data Factory which seems to be so far working pretty wellā€¦

But yeah, gotta switch back to using mockjax for the moment as all my other integration-style tests seem to fall apart when Iā€™m using this library. Not sure why.

1 Like

Sadly a change in 1.4+ broke this and hasnā€™t been fixed. Not sure if there is an ETA on it. I havenā€™t been able to muster enough support to convince anyone this was a ā€œbreaking changeā€, so it remains unfixed :disappointed:

@trek, any idea which aspects caused the breakage? Iā€™d be happy to help out fixing it up, but I have not been able to figure out which changes are causing the issue.

I really like the fakehr / httpRespond approach.

https://github.com/trek/ember-testing-httpRespond/pull/13 is the Issue on the library itself, related to the change in this PR on Ember: https://github.com/emberjs/ember.js/pull/4347

In the meantime, I also made https://github.com/trek/pretender which does the fake server approach. Itā€™s based on route-recognizer and FakeXMLHttpRequest so I personally think it fits better into the modern JS ecosystem of smaller parts compared to ā€œone library, many coupled partsā€ pattern of yesteryear.

Yes, the irony of saying that and working on Ember doesnā€™t escape me.

1 Like

That looks really nice. One thing Iā€™ve been using (today, actually) is the data object (ie query params on the URL) for mockjaxā€¦ because my API responds differently depending on the passed through query paramsā€¦

And also, Iā€™ve been using the fact that mockjax allows you to clear individual mockjax setupsā€¦ which is incredibly handy in mocking the changing state of data as I click through data-changing URLs (ie click this, change that data, now Ember-Data expects a request to that URL to be with the updated data, etc.) ā€“ hopefully you know what I mean here.

I was wondering if pretender would afford those things? Using mockjax is pretty clunky and Iā€™d way prefer to use your httpRespond library.

Hey @rwjblue thanks very much for mentioning this. Iā€™m using it to great effect :slight_smile: :+1:

Hey @trek Iā€™m pretty interested in how your original mocking idea might work for cases where there are actually 4 or 5 models that ember data loads when you, say, click a button? Would you just specify all of the responses?

Or would you have a combo of the server mock with the response mock, or the response as an override? I think that would be awesomeā€¦ like a hybridā€¦ server, but then the response ā€œupdatesā€ the server. LOL :smile:

Hi @mfeckie , I started reading your book today and was initially encouraged to read about @trekā€™s https://github.com/trek/ember-testing-httpRespond in chapter 9. Of course, a little more research made me realise it is currently broken due to changes introduced in Ember 1.4. What are you using in the mean time to fake HTTP requests? Are you using https://github.com/trek/pretender?

Yeah @keithpitty , Iā€™m sad about the breakage too! Messed up my plan in a big way! Anyway, thatā€™s progress I guess.

I am indeed looking at pretender as the replacement, but am still familiarising myself with the API and consider how it will fit into the overall structure of the book.

Iā€™ve been slow on the next update due to a number of significant changes to Ember, Teaspoon and Rails 4.1! Iā€™m catching up and will hopefully be ready to release the next version in the next month or so.

Hey, so if you are running on 1.5, (wheter via upgrade or greenfield) then it should be working now. I think the patch was done a month ago or so. Iā€™m not sure but I kinda remember it being mentioned that it wasnā€™t the most ideal patch though.

There is a project that I created to help with exactly what you are wanting to do. It creates fixtures from factories for ember data. It supports RESTAdapter/AMAdapter, has test helpers ( that use mockjax ) to make integration (or unit) testing pretty fun and easy. The whole story is here:

https://github.com/danielspaniel/ember-data-factory-guy

Check it out and let me know what you think.

1 Like

I started out using teddyzeenny/ember-data-factory and ran into issues were my test setups were becoming monstrously large. Then @Kerrick told me about @danielspanielā€™s ember-data-factory-guy and it reduce the test setup bloat and the overall testing run time in about half.

It also has benefits over ember-data-factory, like having sequences baked into the library.