如何在Capybara中使用测试数据库? [英] How to use the test database with Capybara?
问题描述
我正在使用RSpec,Spork,Capybara和Capybara Mechanic为使用Facebook connect的注册路径编写集成测试。它可以正确地进行注册,但是新用户是在我的开发数据库中创建的,而不是在测试数据库中创建的。
I'm using RSpec, Spork, Capybara and Capybara Mechanic to write integration tests for a registration path that uses Facebook connect. It correctly navigates through registration, but the new users are created in my development database instead of the test database.
RSpec已设置为使用测试环境,并且我已经确认我在规范中运行的所有模型方法都可以测试数据库,但是所有UI操作都可以开发。
RSpec is setup to use the test environment and I've confirmed that any model methods I run in my spec all hit the test database, but all of the UI actions hit development.
这是我的测试:
it "go through registration path" do
print "RAILS_ENV: #{Rails.env}\n" # correctly prints 'test'
print "1) #{User.count}\n" # correctly shows the user count in my test database (zero)
Capybara.current_driver = :mechanize
visit root_path
click_link "register"
# Fill in Facebook Connect form
fill_in 'email', :with => "bob@example.com"
fill_in 'pass', :with => "password"
click_button "Log In"
print "2) #{User.count}\n" # still shows zero users in the test database
end
在测试之后,我可以查看我的开发数据库,并在那里创建了新用户。
After that test I can look at my development database and the new user has been created there.
我也尝试过设置database_cleaner,但这并没有帮助。
I've tried setting up database_cleaner as well and that hasn't helped.
任何人都知道我在做什么是否需要这样做,才能使capybara进入测试数据库?
Anyone know what I need to do so that capybara will hit the test database?
rails 3.1.3
rails 3.1.3
rspec 2.7.0
rspec 2.7.0
猪肉0.9.0
水豚1.1.0
capybara-mechanize 0.3.0.rc3
capybara-mechanize 0.3.0.rc3
推荐答案
问题是我有一个单独的开发服务器与测试和测试默认使用相同的地址/端口。我通过在spec_helper.rb中添加以下内容来解决此问题,以便Capybara使用其他端口。
The problem was that I had a separate development server running in parallel with the tests and the tests were defaulting to a the same address/port. I resolved this by adding the following to spec_helper.rb so Capybara would use a different port.
Capybara.run_server = true
Capybara.server_port = 7000
Capybara.app_host = "http://localhost:#{Capybara.server_port}"
这篇关于如何在Capybara中使用测试数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!