Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSpec test broken by pagination (Kaminari)

Tags:

I have a view spec that was passing but is broken now that pagination (via Kaminari gem) has been added to the view. I'm still trying to get my head around RSpec's syntax...so looking for help in getting this to pass as the page works fine in the browser. I'm aware that many people frown on view specs for being brittle (probably for reasons like this) but I still would like to keep this one passing

I am assigning some stubbed posts to the @posts array. But arrays don't respond to current_page. So how should I handle this in RSpec?

Failures:    1) posts/index.html.haml renders a list of posts      Failure/Error: render      ActionView::Template::Error:        undefined method `current_page' for #<Array:0x000001028ab4e0>      # ./app/views/posts/index.html.haml:31:in `_app_views_posts_index_html_haml__291454070937541541_2193463480'      # ./spec/views/posts/index.html.haml_spec.rb:39:in `block (2 levels) in <top (required)>' 

spec/views/posts/index.html.haml_spec.rb:

require 'spec_helper'  describe "posts/index.html.haml" do   before(:each) do     ...     assign(:posts, [       Factory.stub(:post),       Factory.stub(:post)     ])         view.should_receive(:date_as_string).twice.and_return("June 17, 2011")     ...   end    it "renders a list of posts" do     render     rendered.should have_content("June 17, 2011")     ...   end end 
like image 323
Meltemi Avatar asked Aug 16 '11 14:08

Meltemi


2 Answers

You can also do something like below:

assign(:posts, Kaminari.paginate_array([         Factory.stub(:post),         Factory.stub(:post)       ]).page(1)) 
like image 69
manojlds Avatar answered Oct 11 '22 00:10

manojlds


You should stub the behavior, try this:

before(:each) do   ...   posts = [Factory.stub(:post), Factory.stub(:post)]   posts.stub!(:current_page).and_return(1)   posts.stub!(:total_pages).and_return(2)   assign(:posts, posts)       view.should_receive(:date_as_string).twice.and_return("June 17, 2011")   ... end 
like image 22
apneadiving Avatar answered Oct 11 '22 00:10

apneadiving