我试图在 Rspec 3.0、Capybara 2.3 中存根 Spree 的 current_user 方法,但无济于事。我的目标是测试页面中只应在用户登录时出现的内容。如何在功能规范中存根 spree_current_user 助手?
功能规格
#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
控制器
class Designers::SpreeVariantsController < ApplicationController
def create
...
@variant.attribute = spree_current_user.first_name #line 14
...
end
end
错误
Failure/Error: click_button 'Create'
NoMethodError:
undefined method `first_name' for nil:NilClass
# ./app/controllers/designers/spree_variants_controller.rb:14:in `create'
在存根该方法时,我也尝试过:
#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'