Skip to content

Instantly share code, notes, and snippets.

@croaky
Created February 23, 2009 16:15
Show Gist options
  • Save croaky/69026 to your computer and use it in GitHub Desktop.
Save croaky/69026 to your computer and use it in GitHub Desktop.
Testing will_paginate with custom Shoulda macros. Depends on Mocha.
class Test::Unit::TestCase
def paginate(collection)
WillPaginate::Collection.create(1, 10) do |pager|
pager.replace(collection)
pager.total_entries = 25
end
end
# Example:
# context "Given an audience in the database" do
# setup do
# @audiences = paginate([Factory(:audience)])
# Audience.stubs(:paginate).returns(@audiences)
# end
#
# context "on GET to #index" do
# setup { get :index }
# should_paginate { @audiences }
# end
# end
def self.should_paginate(&block)
model = self.name.gsub(/ControllerTest$/, '').singularize
if model.include?('::')
model = model[model.rindex('::')+2..model.size]
end
model = model.constantize
before_should "paginate" do
collection = instance_eval &block
model.expects(:paginate).returns(collection)
end
end
# Example:
# should_display_page_entries_info_for { @audiences }
def self.should_display_page_entries_info_for(&block)
before_should "display page entries info" do
collection = instance_eval &block
stubbed_action_view.expects(:page_entries_info).at_least_once.with(collection)
end
end
def self.should_display_pagination
should "display pagination" do
assert_select "div.pagination", { :minimum => 1 },
"View isn't displaying pagination. Add <%= will_paginate @collection %>."
end
end
# Example:
# context "a GET to index not logged in as admin" do
# setup { get :index }
# should_not_display_pagination
# end
def self.should_not_display_pagination
should "not display pagination" do
assert_select "div.pagination", { :count => 0 },
"View is displaying pagination. Check your logic."
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment