我試圖在Rails 4項目上使用capybara-webkit的Rspec,並且我得到一個錯誤以便與Capybara.default_host沒有正確設置。Rails 4,RSpec,水豚&webkit_server
webkit_server已正確安裝並位於PATH中。
我已經添加了以下寶石的Gemfile:
group :test do
gem 'rspec-rails', "~> 2.12"
gem 'factory_girl'
gem 'capybara', '2.0.3' # capybara-webkit works with this version
gem 'capybara-webkit'
gem 'database_cleaner', :git => 'git://github.com/bmabey/database_cleaner.git'
gem 'email_spec'
end
我spec_helper.rb包含除其他事項外:
require 'capybara'
require "capybara-webkit"
RSpec.configure do |config|
config.include(EmailSpec::Helpers)
config.include(EmailSpec::Matchers)
config.include Capybara::DSL, :type => :request
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.start
end
end
Capybara.default_host = '127.0.0.1:3001'
Capybara.run_server = true
Capybara.javascript_driver = :webkit
這裏是我的測試:
require 'spec_helper'
feature 'A feature' do
include Capybara::DSL
scenario 'first scenario', :js => true do
visit login_url
page.current_url.should eq('http://127.0.0.1:3001/login')
...
end
end
的錯誤我得到的是:
[16:40:24] [email protected]:web $ rspec spec/features/my_spec.rb
Run options: include {:locations=>{"./spec/features/my_spec.rb"=>[165]}}
F
Failures:
1) A feature - first scenario
Failure/Error: page.current_url.should eq('http://127.0.0.1:3001/login')
expected: "http://127.0.0.1:3001/login"
got: "http://example.iana.org/"
(compared using ==)
# ./spec/features/my_spec.rb:8:in `block (2 levels) in <top (required)>'
Finished in 1.69 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/features/my_spec.rb:8 # A feature - first scenario
Randomized with seed 54529
看起來像它的使用example.org因爲這是在測試環境中的默認的主機名。
有什麼建議嗎?
更新:原來,這不是一個錯誤,但它是由設計。該解決方案是永遠不會使用* _url但* _path(絕對和相對):
visit login_path
page.current_path.should eq(login_path)
謝謝比利。這似乎是這種情況,雖然它不是一個錯誤,但通過設計完成。請參閱:https://groups.google.com/forum/?fromgroups=#!topic/ruby-capybara/HMKCIDJAA6w – 2013-05-10 10:25:10