【问题标题】:how to run capybara commands once, then run some tests如何运行一次 capybara 命令,然后运行一些测试
【发布时间】:2012-09-11 14:06:45
【问题描述】:

我有给定的测试代码:


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

现在我想添加更多测试。 为避免代码加倍,如何在所有测试之前运行一次水豚登录代码? 一种解决方案是将登录代码放在 before 方法中。另一种方法是创建一个 do_login 方法,将代码放入其中并像这样运行每个测试:

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

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

我怎样才能运行一些水豚代码,然后在此之后进行所有测试?

【问题讨论】:

    标签: ruby testing rspec capybara rspec2


    【解决方案1】:

    您不能运行一次 capybara 代码然后运行所有测试。你总是从头开始。您提出的使用 before(:each) 或辅助方法的解决方案是唯一的可能性。 (可以在 before(:all) 之前运行一些 ruby​​,例如在事务之外创建对象 check here 但不是 Capybara)

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

    如果您使用的是设计检查设计维基:https://github.com/plataformatec/devise/wiki/How-To:-Controllers-tests-with-Rails-3-(and-rspec)

    【讨论】:

    • 感谢您的回答。你知道为什么它不起作用吗?是水豚的问题还是其他的问题?
    • 浏览器在每个规范中被重置。这不是错误,而是功能。
    猜你喜欢
    • 2020-07-27
    • 1970-01-01
    • 2014-09-22
    • 1970-01-01
    • 1970-01-01
    • 2014-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多