![](/img/trans.png)
[英]Cucumber not finding a route in test mode that I can see in development mode
[英]Cucumber not using a route in test mode that I can use in development mode
在開發模式下,我可以將瀏覽器指向http://arewesmallyet.dev/data,然后看到所有已抓取數據的json轉儲,表明路由映射和應用程序中的數據操作正在執行它們的工作。
在/features/step_definitions/landing_steps.rb中,我有:
When /^I visit the (.*) page$/ do |webpage|
visit path_to(webpage)
end
在/features/support/url.rb中,我有:
def path_to(webpage)
case webpage
when 'home'
'/'
when 'data'
'/data'
end
end
rake routes
給出:
Application: Arewesmallyet
URL REQUEST PATH
(:index) GET /
(:data) GET /data(.:format)
但是當我運行黃瓜時,我得到:
Scenario: data page # features/landing.feature:10
Given records exist in the database # features/step_definitions/landing_steps.rb:9
When I visit the data page # features/step_definitions/landing_steps.rb:1
File saved to ~/Developer/Ruby/arewesmallyet/capybara-timestamp.html.
Please install the launchy gem to open the file automatically.
Then I should be served the json data # features/step_definitions/landing_steps.rb:15
proper content missing (Minitest::Assertion)
features/landing.feature:13:in `Then I should be served the json data'
capybara-timestamp.html的內容來自“ /”。 當我puts path_to(webpage)
添加到步驟中時,我得到了正確的打印路徑。 但是current_url
給出'/'。
實際上,如果我將步驟更改為:
When /^I visit the (.*) page$/ do |webpage|
puts path_to(webpage)
visit path_to(webpage)
puts 'first:'+current_path
end
(截斷的)輸出為:
When I visit the data page # features/step_definitions/landing_steps.rb:1
/data
first:/
我應該如何找到這個問題的原因?
我在使用byebug進行調試時發現了一些有趣的東西:
當我告訴Capybara訪問“ /數據”,“ / data.json”,“ / data.js”,“ /”或任何其他路徑時,它正在嘗試訪問http://www.example.com 。 由於所有路徑都變成了http://www.example.com ,因此該網址沒有路徑組件,因此我的應用顯然可以使用“ /”。 我不想訪問遠程URL,這就是為什么我使用'/'和'/ data'路徑以及:rack_test
驅動程序的原因; 但根據https://groups.google.com/forum/#!topic/ruby-capybara/HMKCIDJAA6w上的討論,水豚完全被打破了嗎?
有任何解決方法,還是這枚寶石只是一文不值?
我在https://groups.google.com/forum/#!topic/ruby-capybara/SaB81spfil8上報告了此問題,我們將看看他們是否願意進行修復。
您可以使用byebug進行調試,然后進入代碼以查看發生了什么。 將byebug添加到Gemfile中的development,test組並進行捆綁安裝。 然后,在進行訪問之前,在步驟定義中添加一個byebug語句。
我還要在您的控制器中添加一條Byebug語句。 基本技術是找出問題,然后介入調查。 在這種情況下,您可能必須介入很多水豚和機架代碼才能解決此問題。
話雖如此,我懷疑問題出在你的路線上。 我猜想,與您在測試時使用的驅動程序相比,瀏覽器在解釋路由方面更加自由。 因此,我嘗試將數據路由更改為“ data.json”或data.html,然后看看會發生什么。 如果這樣不起作用,請將route.rb的內容添加到您的問題中
如果您未設置app_host,則Capybara會故意使用example.com,並且由於如果域不正確,我會重定向,因此這是不可接受的,因此我必須在我的features/support/env.rb
文件中設置app host,如下所示:
Capybara.app_host = 'http://arewesmallyet.dev'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.