简体   繁体   English

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

[英]Testing Ember Simple Auth in Ember App Kit with sinon

I would like to mock server login responses when testing Ember Simple Auth in my Ember App Kit application.在我的 Ember App Kit 应用程序中测试 Ember Simple Auth 时,我想模拟服务器登录响应。 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. #identification 和#password 输入字段存在,并且包含它们的字段上存在提交按钮。

I am including sinon and qunit in my headers.我在我的标题中包含 sinon 和 qunit。 Am I calling sinon the wrong way or making some other mistake?我是在用错误的方式调用 sinon 还是犯了其他错误?

Edit: Resolution: By also including sinon-qunit the problem disappeared.编辑:解决方案:通过还包括 sinon-qunit,问题就消失了。 It seems like you can not use sinon with Ember App Kit qunit tests without including sinon-qunit.如果不包含 sinon-qunit,您似乎无法将 sinon 与 Ember App Kit qunit 测试一起使用。

Edit 2: I open sourced an example with tests that mocks login responses with sinon here: https://github.com/digitalplaywright/eak-simple-auth编辑 2:我开源了一个示例,其中包含使用 sinon 模拟登录响应的测试: https : //github.com/digitalplaywright/eak-simple-auth

I open sourced an example with tests that mocks login responses with sinon here at https://github.com/digitalplaywright/eak-simple-auth我在https://github.com/digitalplaywright/eak-simple-auth 上开源了一个带有测试的示例,该示例用 sinon 模拟登录响应

The example is made using Ember App Kit, Ember Simple Auth, and Ember.该示例是使用 Ember App Kit、Ember Simple Auth 和 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');
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM