testing - Ember-auth 登录测试因 json 失败

标签 testing asynchronous ember.js qunit

我在测试我的登录/注销和我的应用的相关功能时遇到了一些问题。该应用程序运行正常,但测试失败。

为了测试,我使用了 QUnittestem(我也试过茶匙)

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. 存在[通过]退出按钮

此外,我还通过使用 mockjax 模拟 ajax 请求来运行测试,但同样失败。

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

最佳答案

我很确定你没有等到第一次访问发生,所以我是这样读的(我不是 CS 人)

  1. 你要让 Ember 去图书馆
  2. 在确定它已完成导航之前,您尝试填写 2 个字段并单击一个按钮(所有这些可能都不存在)
  3. 然后您检查它是否是图书馆,但是在您认为您点击之后等待时,该页面实际上完成了从访问中呈现登录页面

这是 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 知道的任何计时器/ promise ,影响 Ember 认为它立即完成了登录过程。因此,点击 promise 会立即得到解决,您会立即运行测试(然后等待全局测试 promise 得到解决)。为了测试这个理论,你可以做一些可怕的超时,看看它是否确实在一段时间后重定向

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

关于testing - Ember-auth 登录测试因 json 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20912193/

相关文章:

c++ - 什么时候使用 std::launch::deferred?

javascript - 如何使用异步操作 WinJS 在 for 循环后执行操作

python - Python请求-线程/进程与IO

reactjs - 开 Jest Jenkins 报道

jquery - 模拟 Scala.js 类

ruby-on-rails - Rails 4.2 集成测试 - 有什么方法可以重用测试代码吗?

javascript - 从 Ember.js 中的嵌套对象计算属性

javascript - 如何通过复选框在模型的数组/内容上实现多个过滤器

ember.js - 如何从 ember-CLI 判断 ember.js 和 ember-data 版本?

在 Hadoop 平台上测试