Question

I would like to mock server login responses when testing Ember Simple Auth in my Ember App Kit application. However, with the following code I get an opaque error 'Unexpected end of input' when the click action is called on the visit function :

var App;

module('Acceptances - SignIn', {
  setup: function(){
    App = startApp();
    this.xhr                = sinon.useFakeXMLHttpRequest();
    this.server             = sinon.fakeServer.create();
    this.server.autoRespond = true;
    sinon.spy(Ember.$, 'ajax');


    this.server.respondWith('POST', '/oauth/token', [
      200,
      { 'Content-Type': 'application/json' },
      '{"access_token":"secret token 2!","token_type":"bearer","expires_in":7200}'
    ]);

  },
  teardown: function() {
    Ember.run(App, 'destroy');
  }
});

test('authentication works correctly', function() {   
  visit('/login').fillIn('#identification', "foo@bar.com").fillIn('#password', "password").click('button[type="submit"]').then(function() {
    ok(!exists('a:contains(Login)'), 'Login button is not displayed when authenticated');
  });
});

The #identification and #password input fields exists, and the submit button exists on the field that contains them.

I am including sinon and qunit in my headers. Am I calling sinon the wrong way or making some other mistake?

Edit: Resolution: By also including sinon-qunit the problem disappeared. It seems like you can not use sinon with Ember App Kit qunit tests without including sinon-qunit.

Edit 2: I open sourced an example with tests that mocks login responses with sinon here: https://github.com/digitalplaywright/eak-simple-auth

Was it helpful?

Solution

I open sourced an example with tests that mocks login responses with sinon here at https://github.com/digitalplaywright/eak-simple-auth

The example is made using Ember App Kit, Ember Simple Auth, and Ember.

This is how I use mock login responses in the:

var App;

module('Acceptances - SignIn', {
  setup: function(){
    App = startApp();
    this.xhr                = sinon.useFakeXMLHttpRequest();
    this.server             = sinon.fakeServer.create();
    this.server.autoRespond = true;
    sinon.spy(Ember.$, 'ajax');


    this.server.respondWith('POST', '/oauth/token', [
      200,
      { 'Content-Type': 'application/json' },
      '{"access_token":"secret token 2!","token_type":"bearer","expires_in":7200}'
    ]);

  },
  teardown: function() {
    Ember.run(App, 'destroy');
  }
});

test('authentication works correctly', function() {
  visit('/').then(function() {
    ok(exists('a:contains(Login)'), 'Login button is displayed when not authenticated');
    ok(!exists('a:contains(Logout)'), 'Logout button is not displayed when not authenticated');
  });

  visit('/login').fillIn('#identification', "foo@bar.com").fillIn('#password', "password").click('button[type="submit"]').then(function() {
    ok(!exists('a:contains(Login)'), 'Login button is not displayed when authenticated');
    ok(exists('a:contains(Logout)'), 'Logout button is displayed when authenticated');
  });
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top