On Sat, May 18, 2013 at 7:59 AM, netzfisch <[email protected]> wrote:
> Hi,
> I aim to test for
>
> expect(rendered).to have_selector "div.pagination"
>
>
> in a view spec - using rspec. I did the following:
>
> describe "recurrences/index" do
> let(:recurrences) do
> [ FactoryGirl.create(:**recurrence, scheduled_to: "2012-07-05"),
> FactoryGirl.create(:**recurrence, scheduled_to: "2012-07-12"),
> FactoryGirl.create(:**recurrence, scheduled_to: "2012-07-19") ]
> end
>
> before :each do
> assign(:recurrences, recurrences)
> view.stub(:will_paginate)
> render
> end
>
> it "should have a pagination bar" do
> expect(rendered).to have_selector('div', class: 'pagination')
> end
> end
>
>
> but got an error
>
> expected to find css "div.pagination" but there were no matches
>
> How do I have to stub the 'will_paginate' method, I tried something like
>
> view.stub(:paginate).with(**page:
> 1).and_return(recurrences.**paginate(per_page:
> 2))
>
>
> which brought me a no-method error for 'paginate'! I had a look at
>
> http://mislav.uniqpath.com/**rails/will_paginate-view-**
> testing/ <http://mislav.uniqpath.com/rails/will_paginate-view-testing/>
>
> And also asked at
> https://groups.google.com/forum/?fromgroups=#!topic/will_paginate/i2LCboRZWfs
> But were no successful until now. Any Ideas or could some one
> provide/point me a working example?
>
> Thanks in advance, h-(
>
If I'm reading it correctly, there is a bit of contradiction in Mislav's
blog. He says "You should not test the outcome of paginating finders or the
will_paginate helper. These are will_paginate features and are already
tested in the plugin’s extensive test suite.", but the examples check for
the presence of "div.paginate", which is an outcome of the will_paginate
helper.
That said, assuming you still want to specify this outcome of
will_paginate, I see several differences between your example and Mislav's:
First, yours stubs :will_paginate. You definitely don't want to do that if
your specifying outcomes of will_paginate. Remove
view.stub(:will_paginate)
Next, Mislav's examples stub :paginate on the model, but yours stub
:paginate on the view. I'm assuming that your view code has something like:
<%= will_paginate @recurrences %>
and the controller code has something like:
def index
@recurrences = Recurrence.paginate :page => params[:page]
end
If that's correct, then you want to replace this:
view.stub(:paginate).with(**page:
1).and_return(recurrences.**paginate(per_page:
2))
with
Recurrence.stub(:paginate).with(**page:
1).and_return(recurrences.**paginate(per_page:
2))
Next, Mislav's examples do not interact with the database, but yours does
by using FactoryGirl.create. The whole point of stubbing the data returned
by paginate is to keep the examples running fast, so I'd replace
let(:recurrences) do
[ FactoryGirl.create(:**recurrence, scheduled_to: "2012-07-05"),
FactoryGirl.create(:**recurrence, scheduled_to: "2012-07-12"),
FactoryGirl.create(:**recurrence, scheduled_to: "2012-07-19") ]
end
with
let(:recurrences) do
[ FactoryGirl.build(:**recurrence, scheduled_to: "2012-07-05"),
FactoryGirl.build(:**recurrence, scheduled_to: "2012-07-12"),
FactoryGirl.build(:**recurrence, scheduled_to: "2012-07-19") ]
end
Lastly, if all you care about in this example is that div.paginate gets
rendered, I'd simplify the whole thing to this:
describe "recurrences/index" do
it "should have a pagination bar" do
assign(:recurrences, [ FactoryGirl.create(:**recurrence, scheduled_to:
"2012-07-05") ])
render
expect(rendered).to have_selector('div', class: 'pagination')
end
end
It's much easier to read because everything is in 3 lines in one example,
and there is no reason to have the extra instances of Recurrence to specify
that the 'div.pagination' selector appears (you may not need any, in which
case an empty array will do the job).
HTH,
David
--
You received this message because you are subscribed to the Google Groups
"rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.