user10756
user10756

Reputation: 649

Rspec cannot find custom module

I try to test devise user authentication, the problem I've done everything according to samples, however the code still doesn't work.

spec/support/devise/devise_support.rb

module ValidUserRequestHelper
    def sign_in_as_a_valid_user
        @user ||= Fabricate(:simple_user)
        post_via_redirect user_session_path, 'user[email]' => @user.email, 'user[password]' => @user.password
    end
end

RSpec.configure do |config|
    config.include ValidUserRequestHelper, :type => :request
end

spec/spec_helper.rb

Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

However when I run test, it fails on the calling to `sign_in_as_a_valid_user'

undefined local variable or method `sign_in_as_a_valid_user' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0xc57a0c4>

I don't have idea how to debug this.

The test code is

require 'spec_helper'

describe User do

  before do
        sign_in_as_a_valid_user
  end

...

Upvotes: 1

Views: 1276

Answers (1)

phoet
phoet

Reputation: 18845

when you write this in your rspec configuration

RSpec.configure do |config|
  config.include ValidUserRequestHelper, :type => :request
end

you tell rspec to only include this helper for request spec. those are typically located in spec/request. deriving from the example of your spec that has describe User in it, i assume that you are writing a model spec, typically located in spec/model. so when running the spec, rspec won't include it for that spec!

if you just remove the :type => :request it will get included everywhere. keep in mind, that there is usually a good reason for this kind of restrictions. for example a helper that only works with a fake browser, like it is done in request specs.

Upvotes: 3

Related Questions