bra*_*rad 6 rspec ruby-on-rails rspec-rails
我正在尝试使用rspec在我的current_user(使用修改的restful_authentication auth解决方案)上存根方法.我完全不确定如何在我的控制器规范中访问此方法.current_user本身不起作用.我需要首先获得控制器吗?我该怎么做呢?
使用rails 2.3.5,rspec 1.3.0和rspec-rails 1.3.2
# my_controller_spec.rb
require 'spec_helper'
describe MyController do
let(:foos){ # some array of foos }
it "fetches foos of current user" do
current_user.should_receive(:foos).and_return(foos)
get :show
end
end
Run Code Online (Sandbox Code Playgroud)
产生
NoMethodError in 'ChallengesController fetches foos of current user'
undefined method `current_user' for #<Spec::Rails::Example::ControllerExampleGroup::Subclass_1::Subclass_1::Subclass_2::Subclass_2:0x7194b2f4>
Run Code Online (Sandbox Code Playgroud)
rspec-rails 为您提供了一种controller在控制器示例中使用的方法。所以:
controller.stub!(:current_user).with(:foos).and_return(foos)
应该可以工作。