使用sinon在Ember App Kit中测试Ember Simple Auth

时间:2014-04-10 18:59:56

标签: javascript authentication ember.js sinon ember-app-kit

我想在我的Ember App Kit应用程序中测试Ember Simple Auth时模拟服务器登录响应。但是,使用以下代码,我得到一个不透明的错误'意外的输入结束'当访问函数调用click操作时:

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');
  });
});

#identification和#password输入字段存在,并且提交按钮存在于包含它们的字段中。

我在我的标题中包含了sinon和qunit。我是以错误的方式打电话给sinon还是犯了其他错误?

编辑:解决方案:通过包括sinon-qunit,问题就消失了。看起来你不能在没有包含sinon-qunit的情况下使用sinon和Ember App Kit qunit测试。

编辑2:我打开了一个示例,其中包含使用sinon模拟登录响应的测试:https://github.com/digitalplaywright/eak-simple-auth

1 个答案:

答案 0 :(得分:2)

我打开了一个示例,其中的测试用https://github.com/digitalplaywright/eak-simple-auth

来模拟使用sinon的登录响应

该示例使用Ember App Kit,Ember Simple Auth和Ember。

这是我在:

中使用模拟登录响应的方式
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');
  });
});