Ruby on rails 如何在rails中使用带有capybara gem的特性规范来实现登录的通用方法?

Ruby on rails 如何在rails中使用带有capybara gem的特性规范来实现登录的通用方法?,ruby-on-rails,capybara,rspec2,specifications,Ruby On Rails,Capybara,Rspec2,Specifications,我使用的是rails v.4.0。使用Factory Girl和Capybara gems使用功能规格测试我的视图。这里的问题是,我希望在我的特性规范中有一个用于登录的通用方法。因此,我可以在我的所有特性规范中使用此方法,以避免重复代码。我的方法应该是这样的: feature 'general' do ... def sign_in(user) visit root_path fill_in 'e

我使用的是rails v.4.0。使用Factory Girl和Capybara gems使用功能规格测试我的视图。这里的问题是,我希望在我的特性规范中有一个用于登录的通用方法。因此,我可以在我的所有特性规范中使用此方法,以避免重复代码。我的方法应该是这样的:

    feature 'general' do
        ...
        def sign_in(user)
             visit root_path    
             fill_in 'email', with: user.email
             fill_in 'password', with: user.password
             click_button 'Sign in'
        end
        ...
        scenario 'Create new folder on system' do
             user = FactoryGirl.create(:user)
             sign_in(user)
             ... do more things here with logged in user
        end
        ...
    end
        def sign_in(user)
             visit root_path
             given(:temporaly_user) { User.make(:email=> user.email, :password=> user.password) } 
             fill_in 'email', with: temporaly_user.email
             fill_in 'password', with: temporaly_user.password
             click_button 'Sign in'
        end
module FeatureHelpers
  def sign_in(user)
    visit root_path
    fill_in 'email', with: user.email
    fill_in 'password', with: user.password
    click_button 'Sign in'
  end
end

RSpec.configure do |config|
  config.include FeatureHelpers, :type => :feature
end
我也试着这样做:

    feature 'general' do
        ...
        def sign_in(user)
             visit root_path    
             fill_in 'email', with: user.email
             fill_in 'password', with: user.password
             click_button 'Sign in'
        end
        ...
        scenario 'Create new folder on system' do
             user = FactoryGirl.create(:user)
             sign_in(user)
             ... do more things here with logged in user
        end
        ...
    end
        def sign_in(user)
             visit root_path
             given(:temporaly_user) { User.make(:email=> user.email, :password=> user.password) } 
             fill_in 'email', with: temporaly_user.email
             fill_in 'password', with: temporaly_user.password
             click_button 'Sign in'
        end
module FeatureHelpers
  def sign_in(user)
    visit root_path
    fill_in 'email', with: user.email
    fill_in 'password', with: user.password
    click_button 'Sign in'
  end
end

RSpec.configure do |config|
  config.include FeatureHelpers, :type => :feature
end
它说给定的方法没有定义。给定的方法在其他方法中不起作用


有人能帮我吗?

在spec/support/中创建一个文件,如下所示:

    feature 'general' do
        ...
        def sign_in(user)
             visit root_path    
             fill_in 'email', with: user.email
             fill_in 'password', with: user.password
             click_button 'Sign in'
        end
        ...
        scenario 'Create new folder on system' do
             user = FactoryGirl.create(:user)
             sign_in(user)
             ... do more things here with logged in user
        end
        ...
    end
        def sign_in(user)
             visit root_path
             given(:temporaly_user) { User.make(:email=> user.email, :password=> user.password) } 
             fill_in 'email', with: temporaly_user.email
             fill_in 'password', with: temporaly_user.password
             click_button 'Sign in'
        end
module FeatureHelpers
  def sign_in(user)
    visit root_path
    fill_in 'email', with: user.email
    fill_in 'password', with: user.password
    click_button 'Sign in'
  end
end

RSpec.configure do |config|
  config.include FeatureHelpers, :type => :feature
end