2
votes

I've got an ember.js app set up, I'm using ember.js 1.0.0-rc4 and ember-data 0.13, I'm trying to get konacha setup with mocha.js similar to this https://github.com/dgeb/ember_data_example.

My spec_helper.js

//= require konacha_config
//= require_tree ./templates
//= require application_test
//= require sinon
//= require spec_utils

// Sinon fake server
var server;

// Stub out Konacha.reset()
Konacha.reset = Ember.K;

// Prevent automatic scheduling of runloops. For tests, we
// want to have complete control of runloops.
Ember.testing = true;

// Defer App readiness (it will be advanced in each test below)
App.deferReadiness();

// Prevent the router from manipulating the browser's URL.
App.Router.reopen({location: 'none'});

beforeEach(function(done) {
  // Fake XHR
  server = sinon.fakeServer.create();

  Ember.run(function() {

    // Advance Contagion readiness, which was deferred above.
    App.advanceReadiness();

    // Setup is complete when the Contagion readiness promise resolves
    App.then(function() {
      done();
    });
  });
});

afterEach(function() {
  // Reset App state
  App.reset();

  // Restore XHR
  server.restore();
});

The specs that I have are running and passing but in the chrome console, I'm seeing stuff like

x GET http://localhost:3500/posts 404 (Not Found)
x GET http://localhost:3500/comments 404 (Not Found)

Why isn't the sinon fake server stubing out these requests?

Ive tried things like

server.respondWith("GET", "/comments",
  [200, { "Content-Type": "application/json" },
   '{"commemnts":[{"id":1,"text":"Comment 1"},{"id":2,"text":"Comment 2"}]}'
]);

With variations on the url of "/comments.json", "http://localhost:3500/comments and "http://localhost:3500/comments.json

Nothing seems to work.

I'm also tried stubbing out the find method with sinon.stub(App.Comments,"find") but but I still see the 404 errors.

Any idea what's going wrong or the correct way to mock/stub out these requests and return meaningful json?

UPDATE 1

When I set server.autoRespond = true I get

Uncaught Error: Fake XHR onreadystatechange handler threw exception: assertion failed: You have turned on testing mode, which disabled the run-loop's autorun. You will need to wrap any code with asynchronous side-effects in an Ember.run.

This happens even when everything is wrapped in an Ember.run.

Adding server.respond() to the afterEach function Results in the same Fake XHR onreadystatechange error.

Adding

Ember.run(function(){
  server.respond();
});

To the afterEach function gets me back to square 1 with the 404 errors

1
I can see you wrapping your test code in Em.run but what about whatever's performing those GETs (App.advanceReadiness()?)?Bluu
Have you ever figured this out?Mawaheb

1 Answers

0
votes

You have ether set the server to auto respond server.autoRespond = true; or trigger server respond with server.respond();. Otherwise the server get the request but dont do anything.