如何一次运行capybara命令,然后运行一些测试 [英] how to run capybara commands once, then run some tests

查看:90
本文介绍了如何一次运行capybara命令,然后运行一些测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有给定的测试代码:


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,将代码放入其中并运行每个测试,如下所示:

Now I want to add more tests. To avoid code doubling, how can I run the capybara login code once before all tests? One solution would be to put the login code in the before method. Another would be to create a method do_login, put the code in it and run every test like this:

it 'should do something after login' do
  do_login
  #test code here
end

但是对于这两种解决方案,代码都是针对每个测试运行的,这不是我想要的。将登录代码放在 before(:all)上也不起作用。

But for both solutions, the code is run for every test and thats not what I want. Putting the login code in a before(:all) doesn't work, too.

如何运行一些水豚代码一次,然后在此之后进行所有测试?

How can I run some capybara code once and then do all the tests after this?

推荐答案

您不能一次运行水豚代码然后全部运行测试。您总是从头开始。您提出的使用before(:each)或辅助方法的解决方案是唯一的可能。 (可以在(:all)之前运行一些ruby,例如在事务 ,但不是Capybara)。

You can't run capybara code once and then run all the tests. You always start from scratch. Your proposed solution with before(:each) or helper method is the only posibility. (It's possible to run some ruby before(:all) e.g. create objects outside the transaction check here but not Capybara)

为了加快规格,您可以在单独的规格中测试登录功能,然后以某种方式对身份验证进行存根验证,但这取决于您的实现。

To speed up your specs you can test login feature in separate spec and then somehow stub the authentication but it depends on your implementation.

如果您使用的是Devise,请检查devise Wiki: https://github.com/plataformatec/devise/wiki/How-To:-Controllers-tests-with-Rails-3-(和-rspec)

If you are using Devise check devise wiki: https://github.com/plataformatec/devise/wiki/How-To:-Controllers-tests-with-Rails-3-(and-rspec)

这篇关于如何一次运行capybara命令,然后运行一些测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆