Module: KaminariRspec::TestHelpers

Defined in:
lib/kaminari_rspec/test_helpers.rb

Instance Method Summary collapse

Instance Method Details

#stub_pagination(resource, options = {}) ⇒ Object

Stubs the paginations method on the resource passed.

Parameters:

  • resource (Object)

    A single object or a collection of objects

  • options (Hash) (defaults to: {})

    The options controlling the stub behaviour

Options Hash (options):

  • :mock (String)

    the mocking framework to be used, defaults to automatic detection based on RSpec configuration

  • :current_page (Integer)

    the desired current page number. Defaults to 1

  • :per_page (Integer)

    the desired amount of elements per page. Defaults to 25

  • :total_count (Integer)

    the desired total amount of elements (used to calculate the last page link)



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/kaminari_rspec/test_helpers.rb', line 17

def stub_pagination(resource, options={})
  return nil unless resource
  options[:current_page] ||= 1
  options[:per_page] ||= 25
  mock_framework = options[:mock] || discover_mock_framework
  values = calculate_values(resource, options)
  wrapped_resource = wrap_resource(resource, options)
  case mock_framework
    when :rspec then stub_pagination_with_rspec(wrapped_resource, values)
    when :rr then stub_pagination_with_rr(wrapped_resource, values)
    when :mocha then stub_pagination_with_mocha(wrapped_resource, values)
    when :flexmock then stub_pagination_with_flexmock(wrapped_resource, values)
    when :nothing then resource
    else
      raise ArgumentError, "Invalid mock argument #{options[:mock]} / framework not supported"
  end
end