Question

I was using rails v.4.0. with Factory Girl and Capybara gems for test my views using features specs. The problem here is that I want to have a generic method in my features specs for sign in. So I could use this method in all my features specs avoid repeating code. My method should be like this:

    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

I also tried to do some like this:

        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

It says that given method is not defined. Given method doesn't work inside other method.

Can anybody help me?

Was it helpful?

Solution

Create a file in spec/support/like this:

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top