1

In the app I'm working with, we have a GET route that validates a user's email address. If the email is invalid, the server responds with:

  • a 200 status code
  • response headers with Content-Type:application/json; charset=utf-8
  • and the response data itself is just a string of "This email is invalid"

I'm trying to simulate this in ember-cli-mirage by doing:

this.get('/ember_api/v1/validations/validate_email', function() {
  return [200, { 'Content-Type': 'application/json' }, "This email is invalid"];

  // also tried this:
  // return new Mirage.Response(200, { 'Content-Type': 'application/json' }, JSON.stringify({"message":"This email is invalid"}));

  // and tried this:
  // return "This email is invalid";
});

The test itself is a button click that fires off this request:

GET "/ember_api/v1/validations/validate_email?email=fakey%40fakefakefake.com&skip_uniq=true"

...and the error I'm getting is:

Pretender intercepted GET /ember_api/v1/validations/validate_email?email=tom%40gmail.com&skip_uniq=true but encountered an error: Nothing returned by handler for /ember_api/v1/validations/validate_email?email=tom%40gmail.com&skip_uniq=true. Remember to return [status, headers, body]; in your route handler.`

It's asking me to return [status, headers, body], but I'm doing this in my handler, and it still throws the error.

Is this actually an issue with the response? Do I need to edit my API to actually return a JSON API formatted object, so I can write the test that way?

I feel like I should be able to return a string in my test since that's what the app is doing. Any help is appreciated.

jacefarm
  • 6,747
  • 6
  • 36
  • 46
Tom Netzband
  • 1,110
  • 1
  • 6
  • 13

1 Answers1

0

The this.get you are using is the Mirage version. You can also use this.pretender.get which should work with your current code sample ...

acorncom
  • 5,975
  • 1
  • 19
  • 31
  • That worked, which is strange to me. If I've installed `ember-cli-mirage`, then shouldn't "the Mirage version" be working? Why do I need to do `this.pretender.get`? I don't see anything referencing this method in the mirage docs. The very last example in the [doc here](http://www.ember-cli-mirage.com/docs/v0.1.x/acceptance-testing/) shows an override using `server.put` instead of `server.pretender.put` – Tom Netzband Nov 30 '16 at 14:04
  • @TomNetzband Mirage wraps pretender with it's own additional simplified layer (which is why your initial approach wasn't working, as you were talking "Pretender" instead of "Mirage". But it allows you to move down a layer and use pretender if desired (http://www.ember-cli-mirage.com/docs/v0.2.x/configuration/#pretender) – acorncom Dec 02 '16 at 17:06