Where/how to include helper methods for capybara integration tests

早过忘川 提交于 2019-12-20 09:45:27

问题


I"m using capybara for my integration/acceptance tests. They're in /spec/requests/ folder. Now I have a few helper methods that I use during acceptance tests. One example is register_user which looks like this

def register_user(user)
  visit home_page
  fill_in 'user_name', :with => user.username
  fill_in 'password', :with => user.password
  click_button 'sign_up_button'
end

I want to use this method in several different acceptance tests (they're in different files). What's the best way to include this? I've tried putting it in spec/support/ but it hasn't been working for me. After spending some time on it I realized I don't even know if it's a good way of doing it so I figured I'd ask here.

Note: I am using rails 3, spork and rspec.


回答1:


Put your helper to the spec/support folder and do something like this:

spec/support/:

module YourHelper
  def register_user(user)
    visit home_page
    fill_in 'user_name', :with => user.username
    fill_in 'password', :with => user.password
    click_button 'sign_up_button'
  end
end

RSpec.configure do |config|
  config.include YourHelper, :type => :request
end



回答2:


I used the given solution by @VasiliyErmolovich, but I changed the type to make it work:

config.include YourHelper, :type => :feature



回答3:


Explicit way with ruby

Using include:

# spec/support/your_helper.rb
class YourHelper
  def register_user(user)
    visit home_page
    fill_in 'user_name', :with => user.username
    fill_in 'password', :with => user.password
    click_button 'sign_up_button'
  end
end

describe MyRegistration do
  include YourHelper

  it 'registers an user' do
    expect(register_user(user)).to be_truthy
  end
end


来源:https://stackoverflow.com/questions/8192559/where-how-to-include-helper-methods-for-capybara-integration-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!