Ruby 如何运行一次capybara命令,然后运行一些测试
我有给定的测试代码:Ruby 如何运行一次capybara命令,然后运行一些测试,ruby,testing,rspec,capybara,rspec2,Ruby,Testing,Rspec,Capybara,Rspec2,我有给定的测试代码: describe 'A new user', js: true do before do @new_user = Fabricate.build(:user) end it 'should sign up' do #login code visit '/' click_link 'Login' fill_in 'user[email]', :with => @new_user.email fill_in 'us
describe 'A new user', js: true do
before do
@new_user = Fabricate.build(:user)
end
it 'should sign up' do
#login code
visit '/'
click_link 'Login'
fill_in 'user[email]', :with => @new_user.email
fill_in 'user[password]', :with => @new_user.password
click_button 'Login now'
#login code end
page.should have_content("Hello #{@new_user.first_name}!")
current_path.should == dashboard_path
end
it 'should receive a confirmation mail' do
#same login code again
visit '/'
click_link 'Login'
fill_in 'user[email]', :with => @new_user.email
fill_in 'user[password]', :with => @new_user.password
click_button 'Login now'
mail = ActionMailer::Base.deliveries.last
assert_equal @new_user.email, mail['to'].to_s
end
end
现在我想添加更多的测试。
为了避免代码加倍,如何在所有测试之前运行一次capybara登录代码?
一种解决方案是将登录代码放在before方法中。另一种方法是创建一个do_login方法,将代码放入其中,然后像这样运行每个测试:
it 'should do something after login' do
do_login
#test code here
end
但对于这两种解决方案,代码都是针对每个测试运行的,这不是我想要的。将登录代码放在(:all)之前的中也不起作用
我怎样才能一次运行一些capybara代码,然后再执行所有测试?您不能一次运行capybara代码,然后再执行所有测试。你总是从零开始。使用before(:each)或helper方法提出的解决方案是唯一可行的。(可以在(:all)之前运行一些ruby,例如在事务之外创建对象,但不能在Capybara中创建)
为了加快您的规范,您可以在单独的规范中测试登录特性,然后以某种方式终止身份验证,但这取决于您的实现
如果您正在使用Deviate,请检查Deviate wiki:谢谢您的回答。你知道为什么它不起作用吗?这是水豚的问题还是其他问题?浏览器在每个规范中都会重置。这不是bug,而是特性。