2015-02-10 52 views
3

我在Ember.js 1.10中進行了單一驗收測試,Ember CLI 0.1.12在PhantomJS上失敗,但在Chrome和Firefox中運行良好。我試圖調試這個爲期2天,但我已經沒有想法了。測試的名稱是用戶可以在登錄時查看僅記錄的頁面。基本上,當您沒有登錄,您嘗試訪問classic路線,例如​​會被重定向到start.loginbeforeModel鉤經典路線:PhantomJS中的單個測試失敗,但在Chrome和Firefox中可用

beforeModel: -> 
    if not @controllerFor('application').get 'logged' 
    @transitionTo 'start.login' 

當你在start.login你要給正確的名稱和用戶名,在StartLoginControllerlogIn動作將被調用:在ApplicationController

logIn: -> 
    if not @get('logged') 
    @get('controllers.application').send 'logIn', @get('username'), @get('password'), @get('rememberMe') 

其中要求採取下列行動:

actions: 
    logIn: (username, password, remember) -> 
     if not @get 'logged' 
     $.ajax 
      url: @get('apiURL') + '/auth/login' 
      type: 'POST' 
      data: 
      name: username 
      password: password 
      remember: remember 
      xhrFields: 
      withCredentials: true #ensure CORS 
     .then (response) => 
      if response.success 

      expires = if remember then new Date(response.cookieExpiration) else null 

      $.cookie 'auth_user_id', response.user.id, 
       expires: expires 
       path: '/' 

      $.cookie 'auth_expiration', response.cookieExpiration, 
       expires: expires 
       path: '/' 

      @setProperties 
       logged: true 
       'controllers.auth.userId': response.user.id 

      @transitionToRoute 'classic.index' 
     , => 
      @send 'openModal', 'modal/wrong-credentials' 

     false 

即使在PhantomJS中,此功能仍可正常工作。其他測試通過。操作被正確調用,屬性設置正確,Cookie設置正確。即使beforeModel掛鉤正確調用(或不)transitionTo方法。我認爲PhantomJS的問題是調用某些異步順序,但我在很多地方試過在Ember.runandThen中打包代碼。沒有運氣。

testem.json

{ 
    "framework": "qunit", 
    "test_page": "tests/index.html?hidepassed", 
    "launch_in_ci": [ 
    "PhantomJS" 
    ], 
    "launch_in_dev": [ 
    "PhantomJS", 
    "Chrome", 
    "Firefox" 
    ] 
} 

驗收測試login-test.coffee(失敗的測試是最後一個):從試驗

`import Ember from 'ember'` 
`import startApp from '../helpers/start-app'` 

application = null 

run = Ember.run 

login = -> 
    visit '/' 

    fillIn '[placeholder=Login]', 'test' 
    fillIn '[placeholder=Hasło]', 'test' 

    click '.button-login' 

clearCookies = -> 
    cookies = $.cookie() 

    for cookie of cookies 
    $.removeCookie cookie, 
     path: '/' 

    cookies = document.cookie.split ';' 
    for i in [0...cookies.length] by 1 
    equals = cookies[i].indexOf '=' 
    name = if equals > -1 then cookies[i].substr(0, equals) else cookies[i] 
    document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT" 

module 'Acceptance: Login', 
    setup: -> 
    application = startApp() 

    clearCookies() 

    time = new Date() 

    $.mockjaxSettings.logging = false 

    $.mockjax 
     type: 'POST' 
     url: 'api/v1/auth/login' 
     dataType: 'json' 
     responseText: 
     success: true 
     user: 
      id: 1 
     cookieExpiration: time.setDate time.getDate() + 14 

    $.mockjax 
     type: 'GET' 
     url: '/api/v1/users/1' 
     dataType: 'json' 
     responseText: 
     user: 
      id: 1 

    $.mockjax 
     type: 'GET' 
     url: '/api/v1/statuses' # ?limitOld=10&friends=true&comments[limit]=5 
     data: 
     comments: 
      limit: 5 
     friends: true 
     limitOld: 10 
     responseText: 
     statuses: {} 

    $.mockjax 
     type: 'GET' 
     url: '/api/v1/getRandomQuote' 

    $.mockjax 
     type: 'GET' 
     url: '/api/v1/statuses/?friends=true&count=true' 
     responseText: 
     count: 0 

    $.mockjax 
     type: 'GET' 
     url: '/api/v1/meals' 

    $.mockjax 
     type: 'GET' 
     url: '/api/v1/notifications' 

    $.mockjax 
     type: 'GET' 
     url: '/api/v1/notifications' 
     data: 
     read: false 
     responseText: {} 

    return 

    teardown: -> 
    $.mockjax.clear() 

    clearCookies() 

    run application, 'destroy' 

test 'user lands on default page when he is not logged', -> 
    expect 1 

    visit '/' 

    andThen -> 
    equal currentPath(), 'start.login' 

test 'login page is displayed when you are trying to access logged-only page', -> 
    expect 1 

    visit '/kitchen' 

    andThen -> 
    equal currentPath(), 'start.login' 

test 'user can login', -> 
    expect 2 

    appController = application.__container__.lookup 'controller:application' 

    equal appController.get('logged'), false, 'user is not logged before login' 

    login() 

    andThen -> 
    ok appController.get 'logged', 'user is logged when response is success' 

test 'user can view logged-only pages when he is logged', -> 
    expect 2 
    console.log '-- LOGGED-ONLY TEST START --' 

    visit '/about' 

    andThen -> 
    equal currentPath(), 'start.login' 

    login() 

    visit '/about' 

    andThen -> 
    equal currentPath(), 'classic.about' 

最後輸出:

TEST'EM 'SCRIPTS!               
Open the URL below in a browser to connect.         
http://localhost:7357/              
━━━━━━━━━━━━━━┓                
PhantomJS 1.9┃ Firefox 37.0            
    198/199 ✘ ┃ 199/199 ✔             
       ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 
Acceptance: Login: user can view logged-only pages when he is logged 
    ✘ failed 
     expected classic.about 
     actual start.login 

-- LOGGED-ONLY TEST START -- 

我不認爲classic.about是錯誤的根源,因爲將其替換爲classic資源的其他子路線導致相同的PhantomJS失敗測試。

好了,看來問題就出在模型ClassicRoute(註釋,使測試通過):

model: -> 
    new Promise (resolve, reject) => 
     @store.find 'user', @controllerFor('auth').get('userId') 
     .then (user) => 
      @controllerFor('auth').set 'user', user 
      resolve 
      profile: user.get 'profile' 
+0

將PhantomJS升級到2.x版可能適合您。 – 2015-02-10 22:53:53

+0

最後的npm版本似乎與PhantomJS 1.9.8打包在一起。似乎PhantomJS 2.0永遠不會在Linux上發佈,因爲它們在準備二進制文件時遇到了一些問題,我必須等待2.1。 – 2015-02-10 22:57:33

回答

2

我在兩個鉤子,而不是一個分裂的邏輯,它現在的作品:

beforeModel: -> 
    if not @controllerFor('application').get 'logged' 
     @transitionTo 'start.login' 
    else 
     @store 
     .find 'user', @controllerFor('auth').get('userId') 
     .then (user) => 
     @controllerFor('auth').set 'user', user 
     @set 'profileId', user.get('profile.id')  

    model: -> 
    @store.find 'profile', @get('profileId') 
+0

嗯,這是離奇的,我看到了很多奇怪的東西到目前爲止測試也不能,你只是從模型鉤返回,而不是手動包裝它的承諾,因爲這將等到承諾完成你 – flylib 2015-02-11 03:44:22

+0

我必須返回模型作爲配置文件屬性的對象 - 這是從'store.find'解決,所以我在這裏有2個承諾。 – 2015-02-11 17:46:18

相關問題