1
votes

I am trying to stub Spree's current_user method in Rspec 3.0, Capybara 2.3, to no avail. My goal is to test the page for content that should only appear when the user is logged in. How can I stub the spree_current_user helper in a feature spec?

Feature Spec

#spec/features/spree_variants_spec.rb
before(:each) do
  user = FactoryGirl.create(:user, first_name: "First name")
  helper.stub :spree_current_user => user # does not work
end

Controller

class Designers::SpreeVariantsController < ApplicationController
  def create
    ...
    @variant.attribute  = spree_current_user.first_name  #line 14
    ...
  end
end

Error

Failure/Error: click_button 'Create'
 NoMethodError:
   undefined method `first_name' for nil:NilClass
 # ./app/controllers/designers/spree_variants_controller.rb:14:in `create'

In stubbing the method, I have also tried:

#2
Designers::SpreeVariantsController.stub :spree_current_user => instance_double(Spree.user_class, :has_spree_role? => true, :last_incomplete_spree_order => nil, :spree_api_key => 'fake', first_name: "First name")

#3
self.stub :spree_current_user => user # same error

#4
helper.stub :spree_current_user => user # does not recognize 'helper'
1
What version of rspec are you using? Are you using rspec-rails at all?Dave Schweisguth
Rspec version 3.0.0 and gem rspec-rails 3.0.1steel

1 Answers

2
votes

RSpec's request and feature specs are designed for testing the full stack, not individual controllers, so you can't get an instance of a controller. It might meet your needs to stub the method on all instances of that controller:

allow_any_instance_of(Designers::SpreeVariantsController).to receive(:spree_current_user).and_return(user)

However,

  • according to the RSpec documentation, request specs don't support current versions of Capybara. If it's working for you in request specs, so far so good, but you may be going down a deprecated path. The up-to-date way of writing specs that exercise your entire stack is feature specs.

  • Request specs and feature specs are for testing the entire stack, not unit testing. Stubbing in them is a little fishy. You should consider writing a few request or feature specs in which the user logs in through the UI, and testing details in controller specs where you should feel free to stub whatever you like.