Nathan Hurst
Nathan Hurst

Reputation: 1750

View helper methods not included for Devise views in rspec integration/request tests?

When I visit my sign in page in a browser everything works fine.

When I visit my sign in page in an rspec integration/request test, I get the following error:

ActionView::Template::Error:
   undefined method `title' for #<#<Class:0x00000007af9180>:0x00000007af32a8>

The title method is used by the view and defined in ApplicationHelper which devise seems to find when using the browser. However, during rspec integration tests, devise is unable to find the helper method.

Is there anything I should be stubbing? It seems wrong to be stubbing in integration tests. Any other ideas?

(This question is not about how to include devise helpers in integration tests. I'm manually filling in the sign in forms to authenticate).

Upvotes: 8

Views: 1567

Answers (3)

Wawa Loo
Wawa Loo

Reputation: 2276

I had a similar problem using Cucumber when I installed devise:

undefined local variable or method `flash_block' for #<#<Class:0x007ffd0a28dae8>:0x007ffd0b2f6d58> (ActionView::Template::Error)

I solved it by including the module in env.rb

Spork.prefork do
  include FlashBlockHelper

I hope this helps.

Upvotes: 2

Chopmo
Chopmo

Reputation: 1416

Looks like this issue. (in some cases related to ActiveAdmin https://github.com/gregbell/active_admin/wiki/Use-spork)

Here I found a hack that works for me (REE 1.8.7, Rails 3.1, Capybara, Devise, active_admin).

However, this is not likely to be merged, so I forked spork-rails to here with that patch applied. And as you probably know I can point my Gemfile to that repo:

gem 'spork-rails', :git => "git://github.com/chopmo/spork-rails.git"

Not optimal but it gets the job done for now.

Upvotes: 7

Chris Barretto
Chris Barretto

Reputation: 9529

Inside /spec/support create devise.rb with this:

RSpec.configure do |config|
  config.include Devise::TestHelpers, :type => :controller
end

Make sure your spec_helper.rb includes:

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

and that your specs have:

require 'spec_helper'

Upvotes: 1

Related Questions