views:

24

answers:

1

I am trying to mock out the session hash for a controller like so:

it "finds using the session[:company_id]" do
  session.should_receive(:[]).with(:company_id).and_return 100
  Company.should_receive(:find).with(100)
  get 'show'
end

When I call get 'show' it states:

received :[] with unexpected arguments  
expected: (:company_id)  
   got: ("flash")

The controller code looks like:

def show
  company_id = session[:company_id]
  @company = Company.find params[company_id]
end

I have also simply tried setting

it "finds using the session[:company_id]" do
  session[:company_id]= 100
  Company.should_receive(:find).with(100)
  get 'show'
end

but then get an issue about:

expected: (100)
got: (nil)

Anyone have ideas why?

A: 

It's because you fetch flash session from your controller. So define it. Flash is save in session.

it "finds using the session[:company_id]" do
  session.stub!(:[]).with(:flash)
  session.should_receive(:[]).with(:company_id).and_return 100
  Company.should_receive(:find).with(100)
  get 'show'
end
shingara
I tried this but I still get an error 1) CompanyController GET 'show' finds using the session[:company_id] Failure/Error: get 'show' undefined method `sweep' for nil:NilClass # /Users/adam/.rvm/gems/ruby-1.8.7-p299/gems/activesupport-3.0.0/lib/active_support/whiny_nil.rb:48:in `method_missing'...
Adam T