2
votes

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', "[email protected]").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

1
Can you close this question then or somehow mark this is solved?marcoow
I'd love to see the details of your solution. I'm attempting to get this to work, but currently if I mock logging in a user more than once within a acceptance test, I end up in an infinite loop of the tests running.David
Sure, I open sourced a project using testing with sinon here: github.com/digitalplaywright/eak-simple-authAndreas Sæbjørnsen

1 Answers

2
votes

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', "[email protected]").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');
  });
});