Ember-auth登录测试失败了json

时间:2014-01-03 20:30:38

标签: testing asynchronous ember.js qunit

我在测试我的登录/注销以及我的应用的相关功能时遇到了一些问题。该应用程序有效,但测试失败。

为了测试,我使用QUnit testem(我也尝试过茶匙)

test "after signin, should redirect user back to previous page", ->
    visit '/library'
    fillIn '.signin-email', 'example@example.com'
    fillIn '.signin-password', 'examplepass'
    click '.signin-btn'
    andThen ->
        equal(testing().path(), 'library', "Should redirect back to library (was #{testing().path()})")

运行测试后,我失败了: (screenshot here

Authentication: visiting restricted page as non authenticated user: after signin, should redirect user back to previous page (2, 0, 2)Rerun544 ms
{user_id: 2, auth_token: wVveiyDLuXBXu69pQ2XQwg}
Source: 
    at Test.QUnitAdapter.Test.Adapter.extend.exception (http://localhost:7357/public/assets/application-aad0a1b2c887cc25124c361787446e83.js:50149:5)
    at superWrapper [as exception] (http://localhost:7357/public/assets/application-aad0a1b2c887cc25124c361787446e83.js:13374:16)
    at Object.onerror (http://localhost:7357/public/assets/application-aad0a1b2c887cc25124c361787446e83.js:50009:22)
    at onerror (http://localhost:7357/public/assets/application-aad0a1b2c887cc25124c361787446e83.js:20453:16)
    at EventTarget.trigger (http://localhost:7357/public/assets/application-aad0a1b2c887cc25124c361787446e83.js:20286:22)
    at null.<anonymous> (http://localhost:7357/public/assets/application-aad0a1b2c887cc25124c361787446e83.js:20439:14)
    at EventTarget.trigger (http://localhost:7357/public/assets/application-aad0a1b2c887cc25124c361787446e83.js:20286:22)
    at http://localhost:7357/public/assets/application-aad0a1b2c887cc25124c361787446e83.js:20588:17
Should redirect back to library (was signin)
Expected:   
"library"
Result: 
"signin"
Diff:   
"library" "signin" 
Source: 
    at http://localhost:7357/public/assets/spec/javascripts/integration/authentication_pages_spec.js.js:22:14
    at andThen (http://localhost:7357/public/assets/application-aad0a1b2c887cc25124c361787446e83.js:50258:20)
    at http://localhost:7357/public/assets/application-aad0a1b2c887cc25124c361787446e83.js:49817:21
    at isolate (http://localhost:7357/public/assets/application-aad0a1b2c887cc25124c361787446e83.js:49989:14)
    at http://localhost:7357/public/assets/application-aad0a1b2c887cc25124c361787446e83.js:49972:12
    at invokeCallback (http://localhost:7357/public/assets/application-aad0a1b2c887cc25124c361787446e83.js:20463:19)
    at null.<anonymous> (http://localhost:7357/public/assets/application-aad0a1b2c887cc25124c361787446e83.js:20513:11)

另外,auth.coffee:

App.Auth = Em.Auth.extend
  request: 'jquery'
  response: 'json'
  strategy: 'token'
  session: 'cookie'
  modules: [
    'emberData',
    'authRedirectable',
    'actionRedirectable'
  ]

  signInEndPoint: '/signin'
  signOutEndPoint: '/signout'
  tokenKey: 'auth_token'
  tokenIdKey: 'user_id'
  tokenLocation: 'param'
  emberData:
    userModel: 'user'  # create user model on login
  authRedirectable:
    route: 'signin'
  actionRedirectable:
    signInRoute: 'library'
    # signInSmart: true
    # signInBlacklist: ['signin']
    signOutRoute: 'index'

我无法找到错误的来源,所以也许它与ember-auth有关。任何想法都将非常感激。

更新1 [1月4日]:

我写了一个额外的测试,只过了一半。该测试比之前更简单,因为它不检查重定向,而只检查登录后用户名是否出现在UI中。

test "after signin, TEST", ->

    visit '/library'
    fillIn '.signin-email', 'user@example.com'
    fillIn '.signin-password', 'foobargaz'
    click '.signin-btn'
    andThen ->
        ok exists('.menu-signout'), "signout button exists"

断言传递,但是我收到一个额外的错误报告返回的JSON,如screenshot所示。截图基本上显示:

  1. [失败] {user_id:2,auth_token:wVveiyDLuXBXu69pQ2XQwg}
  2. [通过]退出按钮存在
  3. 此外,我还通过使用mockjax模拟ajax请求来运行测试,但同样的失败。

    第三,我应该注意到我必须修补“ember-auth-request-jquery.js”以使其与ember测试一起工作here

2 个答案:

答案 0 :(得分:0)

我很确定你在第一次访问时没有等到,所以这就是我读的方式(我不是CS人)

  1. 你告诉Ember去图书馆
  2. 在确定已完成导航之前,您要尝试填写2个字段并单击按钮(所有这些可能都不存在)
  3. 然后你检查它是否是库,但在你想到你点击之后等待,真的是页面完成从访问中呈现登录页面
  4. 这就是js2coffe所说的那种样子(我的主要观点是访问后then)。

    test "after signin, should redirect user back to previous page", ->
      visit("/library").then ->
        fillIn ".signin-email", "example@example.com"
        fillIn ".signin-password", "examplepass"
        click(".signin-btn").then ->
          equal testing().path(), "library", "Should redirect back to library (was " + (testing().path()) + ")"
    

    更新1/4:文档已更改

    现在我们转向有根据的猜测时间。通过Ember-auth代码查看它可能不会创建Ember知道的任何计时器/承诺,因为Ember认为它立即完成了登录过程。因此,点击承诺立即得到解决,您立即运行测试(然后等待全局测试承诺解决)。为了测试理论,你可以做一些可怕的超时,看看它是否确实在一段时间后重定向

    test "after signin, should redirect user back to previous page", ->
      visit "/library"
      fillIn ".signin-email", "example@example.com"
      fillIn ".signin-password", "examplepass"
      click ".signin-btn"
      stop()
      Ember.run.later this, (->
        start()
        equal testing().path(), "library", "Should redirect back to library (was " + (testing().path()) + ")"
      ), 5000
    

答案 1 :(得分:0)

事实证明我的coffeescript并不是世界上最好的。

module中的QUnit函数不应编译为:

module('Authentication: visiting restricted page as non authenticated user', function() {
    return setup(function() {
        return Em.run(App, App.advanceReadiness);
    });

});

但是:

module('Authentication: visiting restricted page as non authenticated user', {
    setup: function() {
        Ember.run(App, App.advanceReadiness);
    },
    // This is also new
    teardown: function() {
        App.reset();
    }
});

此外,在我的spec_helper.coffee文件中,我有类似的内容:

QUnit.testStart(function() {
    // FIXME: this below made it fail every time
    // Ember.run(function() {
    //   return App.reset();
    // });
    Ember.testing = true;
});

QUnit.testDone(function() {
    Ember.testing = false;
});

QUnit.done(function() {
    return Ember.run(function() {
        return App.reset();
    });
});

这似乎引起了一些问题,所以我只是将其删除,现在测试通过了。