2017-03-16 81 views
0

我有一個列出了門票的應用程序。它使用AngularJS。這裏的控制器動作:使用Rspec和PhantomJS測試Rails + AngularJS應用程序時出現奇怪錯誤

def index 
    @tickets = apply_scopes(@tickets) 
    response.headers['x-total-count'] = @tickets.total_count 
    response.headers['x-per-page'] = Ticket.default_per_page 
end 

的角度控制器(CoffeeScript的):

$scope.fetch = -> 
    Ticket.query($scope.search).then (response) -> 
    $scope.tickets = response.data 
    $scope.totalCount = response.headers('x-total-count') 
    $scope.perPage = response.headers('x-per-page') 
$scope.fetch() 

我使用angular-rails-resource獲取的記錄。如果我手工測試,一切都很順利。

這裏是規格:

let(:user) { create :user } 

scenario 'User lists tickets', js: true do 
    login_as user, scope: :user 
    ticket = create :ticket, user: user 

    visit root_path 
    click_on 'Support Requests' 

    expect(page).to have_content(ticket.subject) 
end 

當我運行這個天賦,我只是拿到常規Rspec的失敗消息,因爲條件不成立,但它應該有:

expected to find text "ticket 000" in... 

我認爲它與併發性有關,而Capybara不等待Angular獲取並顯示記錄。然後,我繼續添加一個sleep 2正好高於期望值來測試。當我這樣做,我得到一個不同的錯誤:

Capybara::Poltergeist::JavascriptError: 
    One or more errors were raised in the Javascript code on the page. If you don't care about these errors, you can ignore them by setting js_errors: false in your Poltergeist configuration (see documentation for details). 

    Possibly unhandled rejection: {"data":"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\">\n<HTML>\n <HEAD><TITLE>Internal Server Error</TITLE></HEAD>\n <BODY>\n <H1>Internal Server Error</H1>\n undefined method `split' for 1:Fixnum\n <HR>\n <ADDRESS>\n  WEBrick/1.3.1 (Ruby/2.3.3/2016-11-21) at\n  127.0.0.1:54674\n </ADDRESS>\n </BODY>\n</HTML>\n","status":500,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","url":"/tickets","params":{},"headers":{"Accept":"application/json","Content-Type":"application/json"},"data":{}},"statusText":"Internal Server Error "} 
    Possibly unhandled rejection: {"data":"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\">\n<HTML>\n <HEAD><TITLE>Internal Server Error</TITLE></HEAD>\n <BODY>\n <H1>Internal Server Error</H1>\n undefined method `split' for 1:Fixnum\n <HR>\n <ADDRESS>\n  WEBrick/1.3.1 (Ruby/2.3.3/2016-11-21) at\n  127.0.0.1:54674\n </ADDRESS>\n </BODY>\n</HTML>\n","status":500,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","url":"/tickets","params":{},"headers":{"Accept":"application/json","Content-Type":"application/json"},"data":{}},"statusText":"Internal Server Error "} 
     at http://127.0.0.1:54674/assets/application-713835b1641be632b29f7502c00a879e171bca5d6d06a5f264afcd819c123e76.js:14363 

這裏是我的籌碼:

  • 軌(5.0.2)
  • 水豚(2.12.1)
  • 吵鬧鬼(1.13 0.0)
  • rspec的核(3.5.4)
  • phantomjs 2.1.1

附加信息:

  • 如果我輸出正確的事情結束的控制器動作之前,它就會被輸出。執行正在經歷整個行動;
  • 如果我在獲取門票前正確地輸入了它,它也會輸出。但是,Promise沒有得到解決。

回答

0

我發現問題出在我的分頁標題上(x-total-countx-per-page)。將它們轉換爲字符串是有用的。奇怪的部分是它在開發中工作正常,但不在測試環境中。因此,如果任何人有在未來的這個問題,在我的情況的解決方案是:

def index 
    @tickets = apply_scopes(@tickets) 
    response.headers['x-total-count'] = @tickets.total_count.to_s 
    response.headers['x-per-page'] = Ticket.default_per_page.to_s 
end 

通知.to_s分配頭時被調用。

相關問題