steven_noble
steven_noble

Reputation: 4203

Why can't my Rspec integration tests find my modules?

I'd like to be able to call login_as_admin and login_as_customer at any point in any spec file.

I have a directory full of integration specs:

/spec/features/area_spec.rb
/spec/features/assignment_spec.rb
/spec/features/etc…

Each of which starts with:

require 'spec_helper'
require 'rspec_macros'

I also have /spec/spec_help.rb, which includes:

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require File.dirname(__FILE__) + "/rspec_macros"

And I have /spec/rspec_macros.rb, which includes:

module RspecMacros
  def login_as_admin
    etc…
  end
  def login_as_customer
    etc…
  end
end

Why, then, do I get the following error at the Rspec command line?

 Failure/Error: login_as_customer
 NameError:
   undefined local variable or method `login_as_customer' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_2:0x007fd99f471f50>

Upvotes: 2

Views: 1828

Answers (1)

John Naegle
John Naegle

Reputation: 8247

I believe you either need to make these functions in spec helper or include the modules in your tests.

To include the modules you can do something like this:

module RspecMacros 
  extend ActiveSupport::Concern

  included do
    def login_as_customer
      ... 
    end
  end
end

require 'rspec_macros'
describe MyCoolTest do    
  include RspecMacros
end

You may find it easier to just make them functions in spec helper. You can just add:

def login_as_customer
   ....
end

to the end of spec_helper.rb

Upvotes: 3

Related Questions