I'm a Floridian web engineer who planted his roots in Paris. Some of my favorite past-times are analyzing and optimizing development processes, building solid APIs, mentoring junior devs, and long walks on the beach.

Test a will_paginate Custom Renderer

I was recently working on a project where my employer wanted me to make the site's current pagination work the way the old site's pagination worked. I had recently switched over using will_paginate, and while there are a number of good resources for making customized link renderers: frivolous, thewebfellas are a few. They don't really mention testing.

While searching through the internets, I eventually stumbled upon this on tsigo's github with some specs for his renderers. For me, I just had to test an override of windowedpagenumbers, and link. Setting up the test can be seen easily enough from the github file, but essentially, you just create an instance of WillPaginate::Collection and pass that into whatever renderer you have created along with any options like so:

@renderer = LinkRenderer.new
@collection = WillPaginate::Collection.new(page, per_page, total_entries)
@renderer.prepare(@collection, options, '')

@renderer.method_name.should do_something

You will have noticed that I've stubbed out the url method. That is because we aren't using a real collection of objects and therefore there is no url to generate.