在何处/如何包含用于水豚集成测试的辅助方法

我正在使用capybara进行集成/验收测试。它们位于/spec/requests/文件夹中。现在我在验收测试中使用了一些辅助方法。一个例子是register_user ,看起来像这样

 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 

我想在几种不同的验收测试中使用这种方法(它们在不同的文件中)。 包含此内容的最佳方式是什么? 我已经尝试将它放在spec/support/但它并没有为我工作。 花了一些时间后,我意识到我甚至不知道这是否是一个好方法,所以我想我会在这里问。

注意:我使用的是rails 3,spork和rspec。

将您的助手放到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 

我使用@VasiliyErmolovich给出的解决方案,但我更改了类型以使其工作:

 config.include YourHelper, :type => :feature 

ruby的明确方式

使用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