Partial_doubles check with rails 4 and rspec 3 fixed

I use authlogic for my user authentication, and in my ApplicationController I have "current_user", "current_user_session", etc., defined and set as helper_methods.

I have a very simple view for my main index:

RSpec.describe "main/index.html.erb", :type => :view do
  context "when not logged in" do

    before do
      allow(view).to receive(:current_user).and_return(nil)
    end

    it "has an h1" do
      render
      expect(rendered).to include('h1')
    end

  end
end

The problem is that if in my configuration "mocks.verify_partial_doubles = true", then this causes an impressively large error, since it resets the entire object, and then says below:

  1) main/index.html.erb when not logged in has an h1
     Failure/Error: allow(view).to receive(:current_user).and_return(nil)
       #<#<Class:0x00000104c249d0>:.........
       @rendered_views={}>> does not implement: current_user

Of course, it is recommended that verify_partial_doubles be set to true, but this will fail. I pulled this straight from the documentation:

https://www.relishapp.com/rspec/rspec-rails/v/3-1/docs/view-specs/view-spec#passing-view-spec-that-stubs-a-helper-method

ApplicationHelper, . ApplicationController helper_method, :

helper_method :current_user, ...

def current_user
  return @current_user if defined?(@current_user)
  @current_user = current_user_session && current_user_session.record
end

, verify_partial_doubles, ?

+4
2

:

RSpec.configure do |config|
  config.before(:each, type: :view) do
    config.mock_with :rspec do |mocks|
      mocks.verify_partial_doubles = false
    end
  end

  config.after(:each, type: :view) do
    config.mock_with :rspec do |mocks|
      mocks.verify_partial_doubles = true
    end
  end
end

, stubbing :

allow(view).to receive(:current_user).and_return(nil)

: https://github.com/rspec/rspec-rails/issues/1076

0

All Articles