timpone
timpone

Reputation: 19969

should this code work for including in a helper file to rspec?

I have the following sample code that is using a vanilla spec_helper. I'm trying to use the sign_in_as but am getting an error saying it doesn't exist. I hadn't really seen this config syntax before so I'm not sure if this is being used right. Any help would be appreciated? thx

in helpers/sign_in_helpers.rb

module SignInHelpers
  def say_hello
    puts 'hello'
  end

  def sign_in
    sign_in_as '[email protected]'
  end

  def sign_in_as email
    visit root_path
    fill_in 'Email address', with: email
    click_button 'Sign In'
  end
end     

RSpec.configure do |config|
  config.include SignInHelpers
end

Upvotes: 0

Views: 55

Answers (2)

Billy Chan
Billy Chan

Reputation: 24815

Some errors in your code.

  1. you should have this code on top of the file

    require 'spec_helper'

  2. The place to put the file is spec/support, not spec/helpers

Also, for the following code, better to keep them all inside spec/spec_helper.rb inside the configure block, though your way should work as well.

RSpec.configure do |config|
  config.include SignInHelpers
end

By the way, better to define the type as your helper are for features only, and not going to work in controller tests.

config.include SignInHelpers type: :feature

Upvotes: 1

muttonlamb
muttonlamb

Reputation: 6501

In theory, yes. Your call looks like a valid use of the include syntax

https://www.relishapp.com/rspec/rspec-core/docs/helper-methods/define-helper-methods-in-a-module

Upvotes: 1

Related Questions