[rspec-users] How to mock helpers in view specs ?

François Beausoleil francois.beausoleil at gmail.com
Thu May 17 10:44:41 EDT 2007


Hi all,

I am mocking the following Rails view (inside a partial):
<%= render :partial => "forums/forum",
    :collection => forum_category.forums.readable_by(current_user? ?
current_user : nil) %>

My spec fails with the following message:
1)
ActionView::TemplateError in 'forum_categories/index (anonymous user)
should only render forums accessible to anonymous users'
undefined local variable or method `current_user' for
#<#<Class:0xb6204d90>:0xb6204d54>
On line #21 of app/views/forum_categories/index.rhtml

But in before, I setup the correct stub:

describe "forum_categories/index (anonymous user)" do
  before do
    @forum_category = mock("forum category")
    @forums_proxy = mock("forums proxy")
    @forum = mock("forum")
    @topics_proxy = mock("topics proxy")

    @forum_category.stub!(:forums).and_return(@forums_proxy)
    @forum.stub!(:topics).and_return(@topics_proxy)

    assigns[:forum_categories] = [@forum_category]

    @controller.stub!(:current_user?).and_return(false)
    @controller.stub!(:current_user).and_return(mock("mock user"))
  end

  it "should only render forums accessible to anonymous users" do
    @forums_proxy.should_receive(:readable_by).with(nil).and_return([])
    do_render
  end

  def do_render
    render "forum_categories/index"
  end
end

What am I doing wrong ?

This is on RSpec 0.9.4 and RSpec on Rails 0.9.4.

Thanks !
-- 
François Beausoleil
http://blog.teksol.info/
http://piston.rubyforge.org/


More information about the rspec-users mailing list