François Beausoleil
2007-May-17 14:44 UTC
[rspec-users] How to mock helpers in view specs ?
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/
AFAIK, you need to stub @controller.template instead On 17-May-07, at 7:44 AM, Fran?ois Beausoleil wrote:> 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/ > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
François Beausoleil
2007-May-17 19:46 UTC
[rspec-users] How to mock helpers in view specs ?
Thanks Ruy ! 2007/5/17, Ruy Asan <ruyasan at gmail.com>:> AFAIK, you need to stub @controller.template insteadThat''s exactly what I needed. Thank you very much. There needs to be a mention of this on http://rspec.rubyforge.org/documentation/rails/writing/views.html Bye ! -- Fran?ois Beausoleil http://blog.teksol.info/ http://piston.rubyforge.org/
On 5/17/07, Fran?ois Beausoleil <francois.beausoleil at gmail.com> wrote:> Thanks Ruy ! > > 2007/5/17, Ruy Asan <ruyasan at gmail.com>: > > AFAIK, you need to stub @controller.template instead > > That''s exactly what I needed. Thank you very much. > > There needs to be a mention of this on > http://rspec.rubyforge.org/documentation/rails/writing/views.htmlThere is. Scroll down.> > Bye ! > -- > Fran?ois Beausoleil > http://blog.teksol.info/ > http://piston.rubyforge.org/ > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
François Beausoleil
2007-May-27 12:00 UTC
[rspec-users] How to mock helpers in view specs ?
2007/5/25, David Chelimsky <dchelimsky at gmail.com>:> On 5/17/07, Fran?ois Beausoleil <francois.beausoleil at gmail.com> wrote: > > http://rspec.rubyforge.org/documentation/rails/writing/views.html > > There is. Scroll down.Well, I''m sorry about that. Thanks for pointing my error :) Bye ! -- Fran?ois Beausoleil http://blog.teksol.info/ http://piston.rubyforge.org/