1

We are using rails-csrf in our ember-cli app. The README on rails-csrf says:

Be sure to mock out the call to the csrf server endpoint. Otherwise your tests will fail with

"error while processing route: [route]"

messages in the browser console. For example:

server = new Pretender(function() {
  this.get('/csrf', function(request) {
    return [200, {"Content-Type": "application/json"},
      JSON.stringify({
        "authenticity_token": "token"
      })
    ];
  });
});

I understand the problem here (our integration tests are indeed showing this error) and I understand how Pretender solves it. I've got Pretender installed via ember-cli-pretender.

What I don't understand is how to make sure this code snippet - configuration for a Pretender mock - is working. I have it installed in the setup block of the integration test module, and it gets called, but the error is still present and the tests still aren't passing.

Here's the current non-working state:

module('Integration test', {
  setup: function() {
    App = startApp();

    var server = new Pretender(function() {
      this.get('/our/api/for/csrf', function(request) {
        return [200, {"Content-Type": "application/json"},
          JSON.stringify({
            "authenticity_token": "token" 
            // I've also tried putting a real token from the server here instead of "token"
          })
        ];
      });
    });
  },
  teardown: function() {
    Ember.run(App, App.destroy);
  }
});

The mock is getting called, but whatever it's returning is apparently not enough for rails-csrf. It looks like the beforeModel call in the application route is returning a promise; it's not clear if that's expected and being resolved.

(This question is superficially similar to this older one, but the available tools for handling this in Ember have changed significantly.)

Community
  • 1
  • 1
pjmorse
  • 9,204
  • 9
  • 54
  • 124
  • Instantiating Pretender inside `setup` should be working, that is a correct place to do it. Look for something else that might be off. – givanse Feb 26 '15 at 15:29
  • @givanse that's good to know! I fixed the actual URL we're sending the CSRF request to, as well. I wonder if that `server` var I'm assigning the Pretender instance to is getting referenced anywhere? Or does that matter? – pjmorse Feb 26 '15 at 15:36
  • I've verified that the Pretender mock is getting called. I guess it's not returning what it needs to return? – pjmorse Feb 27 '15 at 19:10
  • Can you setup a jsbin? – givanse Feb 27 '15 at 21:56
  • I've tried setting up a jsbin a few times, but the thicket of errors I've been fighting through (getting the environment set up, basically) just to get to the error I have in my app is horrific. I'll take another stab at it, but I'm not optimistic, especially getting rails-csrf into the mix. – pjmorse Mar 02 '15 at 15:52
  • 1
    I'm also taking a spike into updating versions - we're on ember-cli 0.1.12 and ember 1.8.1, so jumping to 0.1.15 and 1.10.x respectively might be worth trying to see if it clears up something obscure. – pjmorse Mar 02 '15 at 15:56
  • So far the update to ember 1.10.x is looking promising; at the very least I'm getting a different error now. – pjmorse Mar 03 '15 at 20:35

1 Answers1

1

I updated our app from ember-cli 0.1.12 and ember 1.8.1 to ember-cli 0.2.0 and ember 1.10.0. I also updated Pretender to 0.6.0 (the ember-cli-pretender package installed 0.1.0). This didn't solve anything by itself, but it did lead to a telling change in how the integration test failed. Now, Pretender was intercepting data requests and returning an error because I hadn't defined handlers for them.

Error: Pretender intercepted GET /our/api/data/:id but no handler was defined for this type of request

So the issue was no longer Ember but my configuration of Pretender. Once I mocked data requests to the API, we were off and running.

tl;dr make sure you have the latest version of Pretender.

pjmorse
  • 9,204
  • 9
  • 54
  • 124