0

我正在使用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

它说给定的方法没有定义。给定的方法在其他方法中不起作用。

有谁能够帮我?

4

1 回答 1

0

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

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
于 2013-11-06T03:31:26.137 回答