kaminari-rspec 0.14.1 → 0.16.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- ZGZjNzljZTRkMjlkOTU1ZDUzNzgyMTk2M2IyZjEwMzMzYjhiYTNkZA==
5
- data.tar.gz: !binary |-
6
- MmNlMDM5ZTFiNjQyYTQ3MzUzODkyMTVkMjZkODU1M2U2YmQ4YzVhNw==
7
- !binary "U0hBNTEy":
8
- metadata.gz: !binary |-
9
- YzJkYTY0ZmMwNzljNWI1MmQwMzExMTJhYjkzZjZmZmM0NzRlMmEzZGY3MjM0
10
- MmFiMDM3M2U5ZGJiNDg0MWUyYjkzMTk0NjNjMzE0NzhjM2ZlNDEyNWI3N2Zj
11
- ZDhkYWE0ZTQ4YTNlYjU5MGZkOTE1ODYyNDZmYjZiYzBkZTk3Yjk=
12
- data.tar.gz: !binary |-
13
- MTU5YjMwYjJhYzVlOTc4YzA3MTY4YTM4MWRlY2MwNmU4MWU2NWZiYmJhOGM5
14
- OTc5MDQ3NWU4YTk4MjY2OTdjMDUxZDZiNTdjM2RjYWYyMmM5Njc2NzNmMDEy
15
- MDQzMTRjMzcyYTY1ZTNjNjEwNmI5NzdjMzc0NGJkOWM3ZGRmZTg=
2
+ SHA1:
3
+ metadata.gz: 56f7deeae8acd80adacd2543b50f4b9678b13946
4
+ data.tar.gz: 21176d5117f8185275cae357c2a8fd7f3399faeb
5
+ SHA512:
6
+ metadata.gz: 60d6345a21f3b339f085cd33dadaa7a56c0ab5c2871b8858fc65dcd763163bf1f5714ff0b5243619e1d06d00fbe46876ea26ce223f8ebe31441f07f9ba64ef36
7
+ data.tar.gz: df7b7a4cdeee8d71258e3699022e761e002236bdf80c012e76b2b1b47e8922fd9c316a30b8b0387c8bb98c7e150fa73adca9b2e5c76144cdb320d0c35502aec3
data/.gitignore CHANGED
@@ -1,4 +1,6 @@
1
1
  Gemfile.lock
2
2
  pkg
3
3
  .ruby-gemset
4
- .ruby-version
4
+ .ruby-version
5
+ doc
6
+ .yardoc
data/README.md CHANGED
@@ -6,7 +6,7 @@
6
6
  [![Build Status](https://travis-ci.org/nessche/kaminari-rspec.png)](https://travis-ci.org/nessche/kaminari-rspec)
7
7
 
8
8
 
9
- `kaminari-rspec` is a ruby gem that aims at simplifying writing the specs for views where Kaminari is used for pagination
9
+ `kaminari-rspec` is a ruby gem that aims at simplifying writing the specs for views where [Kaminari](https://github.com/amatsuda/kaminari) is used for pagination
10
10
 
11
11
  ## Installing
12
12
 
@@ -26,9 +26,9 @@ to fork this repo and update the version and the code as needed.
26
26
  ## Rendering views with RSpec's render_view
27
27
 
28
28
  If you are rendering the views from your controller spec using render_views AND you are mocking
29
- the data returned from the db with something along the lines of
29
+ the data returned from the db with something along the lines of (RSpec syntax)
30
30
 
31
- Model.stub_chain(:order, :page, :per).and_return([model_mock])
31
+ allow(Model).to receive_message_chain(:order, :page, :per).and_return(model_mock)
32
32
 
33
33
  you may want to also stub the methods needed by the pagination partials, in order to do that, just
34
34
  add the following to your `spec_helper`
@@ -37,17 +37,23 @@ add the following to your `spec_helper`
37
37
 
38
38
  and then modify your controller spec to look like
39
39
 
40
- Model.stub_chain(:order, :page, :per).and_return(stub_pagination([model_mock]))
40
+ allow(Model).to receive_message_chain(:order, :page, :per).and_return(stub_pagination(model_mock))
41
41
 
42
- the stubs will return the values needed to set the pagination as being on the first and only page.
42
+ the stubs will return the values needed to set the pagination as being on the desired page, with the desired
43
+ total page count (see options below how to set these parameters).
43
44
 
44
45
  ### Pagination options
45
46
 
46
- If specific pagination values are needed they can be defined using a hash. The following values are supported:
47
+ If specific pagination values are needed they can be defined using a hash passed as the second parameter to method
48
+
49
+ stub_pagination(resource, options = {})
50
+
51
+ The following values are supported:
47
52
 
48
53
  * `:total_count` : the total number of elements to be paginated. It defaults to `resource.length` if resource is a collection,
49
54
  otherwise to 1
50
- * `:per_page` : the amount of elements per page, defaults to 25
55
+ * `:per_page` : the amount of elements per page, defaults to 25. If `resource` does not contain enough elements to fill
56
+ on page, no padding will occur. If `resource` contains more elements, only the first ones needed to fill the page will be shown
51
57
  * `:current_page` : the current page of the pagination. Defaults to 1. Notice that `current_page` is anyway always
52
58
  set such that it respects the values passed in `total_count` and `per_page`, i.e. if you pass a
53
59
  total count of 95 and a per page value of 10, current page will be capped to 10
@@ -57,7 +63,8 @@ As an example
57
63
  stub_pagination(resource, :total_count => 50, :current_page => 3, :per_page => 10)
58
64
 
59
65
  will create the pagination links for a total count of 50 elements, with 10
60
- elements per page and page 3 being the current_page.
66
+ elements per page and page 3 being the current_page. If `resource` contains more than 10
67
+ elements, only the first 10 will be shown.
61
68
 
62
69
  ## Detecting mocking framework
63
70
 
@@ -20,8 +20,8 @@ Gem::Specification.new do |s|
20
20
 
21
21
 
22
22
  # specify any dependencies here; for example:
23
- s.add_runtime_dependency 'kaminari', '0.14.1'
24
- s.add_development_dependency 'rspec'
23
+ s.add_runtime_dependency 'kaminari', '~> 0.16'
24
+ s.add_development_dependency 'rspec', '~> 3'
25
25
  s.add_development_dependency 'rake'
26
26
  s.add_development_dependency 'yard'
27
27
  s.add_development_dependency 'rr'
@@ -1,39 +1,63 @@
1
+ require 'kaminari'
2
+ require 'kaminari/models/array_extension'
3
+
1
4
  module KaminariRspec
5
+
6
+
2
7
  module TestHelpers
3
8
 
9
+ # Stubs the paginations method on the resource passed.
10
+ #
11
+ # @param resource [Object] A single object or a collection of objects
12
+ # @param options [Hash] The options controlling the stub behaviour
13
+ # @option options [String] :mock the mocking framework to be used, defaults to automatic detection based on RSpec configuration
14
+ # @option options [Integer] :current_page the desired current page number. Defaults to 1
15
+ # @option options [Integer] :per_page the desired amount of elements per page. Defaults to 25
16
+ # @option options [Integer] :total_count the desired total amount of elements (used to calculate the last page link)
4
17
  def stub_pagination(resource, options={})
5
18
  return nil unless resource
19
+ options[:current_page] ||= 1
20
+ options[:per_page] ||= 25
6
21
  mock_framework = options[:mock] || discover_mock_framework
7
22
  values = calculate_values(resource, options)
23
+ wrapped_resource = wrap_resource(resource, options)
8
24
  case mock_framework
9
- when :rspec then stub_pagination_with_rspec(resource, values)
10
- when :rr then stub_pagination_with_rr(resource, values)
11
- when :mocha then stub_pagination_with_mocha(resource, values)
12
- when :flexmock then stub_pagination_with_flexmock(resource, values)
25
+ when :rspec then stub_pagination_with_rspec(wrapped_resource, values)
26
+ when :rr then stub_pagination_with_rr(wrapped_resource, values)
27
+ when :mocha then stub_pagination_with_mocha(wrapped_resource, values)
28
+ when :flexmock then stub_pagination_with_flexmock(wrapped_resource, values)
13
29
  when :nothing then resource
14
30
  else
15
31
  raise ArgumentError, "Invalid mock argument #{options[:mock]} / framework not supported"
16
32
  end
17
33
  end
18
34
 
35
+ private
36
+
19
37
  def discover_mock_framework
20
38
 
21
39
  mock_framework = RSpec.configuration.mock_framework
22
40
  return mock_framework.framework_name if mock_framework.respond_to? :framework_name
23
41
  puts('WARNING: Could not detect mocking framework, defaulting to :nothing, use :mock option to override')
24
- return :nothing
42
+ :nothing
43
+
44
+ end
25
45
 
46
+ def wrap_resource(resource, options)
47
+ return resource if resource.respond_to? :total_count
48
+ wrappable = resource.respond_to?(:length) ? resource : [resource]
49
+ Kaminari::paginate_array(wrappable).page(1).per([wrappable.length, options[:per_page]].min)
26
50
  end
27
51
 
28
52
 
29
- def calculate_values(resource, options={})
53
+ def calculate_values(resource, options)
30
54
 
31
55
  values = {}
32
56
  values[:total_count] = options[:total_count] || (resource.respond_to?(:length) ? resource.length : 1)
33
- values[:limit_value] = options[:per_page] || 25
34
- values[:total_pages] = (values[:total_count] / values[:limit_value]) + ((values[:total_count] % values[:limit_value]) == 0 ? 0 : 1)
35
- values[:current_page] = [(options[:current_page] || 1), values[:total_pages]].min
36
- return values
57
+ values[:limit] = options[:per_page]
58
+ values[:total_pages] = (values[:total_count] / values[:limit]) + ((values[:total_count] % values[:limit]) == 0 ? 0 : 1)
59
+ values[:current_page] = [options[:current_page], values[:total_pages]].min
60
+ values
37
61
  end
38
62
 
39
63
  def stub_pagination_with_rspec(resource, values)
@@ -42,7 +66,7 @@ module KaminariRspec
42
66
  allow(resource).to receive(key).and_return(value)
43
67
  end
44
68
 
45
- return resource
69
+ resource
46
70
 
47
71
  end
48
72
 
@@ -52,7 +76,7 @@ module KaminariRspec
52
76
  eval "stub(resource).#{key} { #{value} }"
53
77
  end
54
78
 
55
- return resource
79
+ resource
56
80
 
57
81
  end
58
82
 
@@ -62,7 +86,7 @@ module KaminariRspec
62
86
  resource.stubs(key).returns(value)
63
87
  end
64
88
 
65
- return resource
89
+ resource
66
90
 
67
91
  end
68
92
 
@@ -74,7 +98,7 @@ module KaminariRspec
74
98
  mock.should_receive(key).zero_or_more_times.and_return(value)
75
99
  end
76
100
 
77
- return mock
101
+ mock
78
102
 
79
103
  end
80
104
 
@@ -1,3 +1,5 @@
1
1
  module KaminariRspec
2
- VERSION = '0.14.1'
2
+
3
+ VERSION = '0.16.1'
4
+
3
5
  end
@@ -1,3 +1,4 @@
1
+ require 'minitest/autorun'
1
2
  require 'flexmock'
2
3
 
3
4
  RSpec.configure do |config|
@@ -1,3 +1,8 @@
1
1
  RSpec.configure do |config|
2
2
  config.mock_framework = :rspec
3
+
4
+ config.mock_with :rspec do |mocks|
5
+ mocks.verify_partial_doubles = true
6
+ end
7
+
3
8
  end
@@ -40,6 +40,14 @@ describe 'KaminariRspec::TestHelpers::' do
40
40
  discover_mock_framework.should == :rspec
41
41
  end
42
42
 
43
+ it 'should correctly mock the pagination' do
44
+ resource = [Object.new, Object.new]
45
+ stubbed_resource = stub_pagination(resource, total_count: 45, current_page: 3, per_page: 10)
46
+ expect(stubbed_resource.total_count).to eq(45)
47
+ expect(stubbed_resource.current_page).to eq(3)
48
+
49
+ end
50
+
43
51
  end
44
52
 
45
53
  end
@@ -13,7 +13,7 @@ describe 'KaminariRspec::TestHelpers' do
13
13
 
14
14
  it 'sets total_count to 1' do
15
15
 
16
- values = calculate_values(Object.new)
16
+ values = calculate_values(Object.new, current_page: 1, per_page: 25)
17
17
  values[:total_count].should == 1
18
18
 
19
19
  end
@@ -24,7 +24,7 @@ describe 'KaminariRspec::TestHelpers' do
24
24
 
25
25
  it 'sets total_count to the length of the object' do
26
26
 
27
- values = calculate_values([ Object.new, Object.new])
27
+ values = calculate_values([ Object.new, Object.new], current_page: 1, per_page: 25)
28
28
  values[:total_count].should == 2
29
29
 
30
30
  end
@@ -38,51 +38,18 @@ describe 'KaminariRspec::TestHelpers' do
38
38
 
39
39
  it 'sets the total_count to the specified value' do
40
40
 
41
- values = calculate_values(Object.new, :total_count => 13)
41
+ values = calculate_values(Object.new, current_page: 1, total_count: 13, per_page: 25)
42
42
  values[:total_count].should == 13
43
43
 
44
44
  end
45
45
 
46
46
  end
47
47
 
48
- context 'when per_page is not specified' do
49
-
50
- it 'set per_page to the default of 25' do
51
-
52
- values = calculate_values(Object.new)
53
- values[:limit_value].should == 25
54
-
55
- end
56
-
57
- end
58
-
59
- context 'when per_page is specified' do
60
-
61
- it 'sets limit_value to the specified value' do
62
-
63
- values = calculate_values(Object.new, :per_page => 17)
64
- values[:limit_value].should == 17
65
-
66
- end
67
-
68
- end
69
-
70
48
  it 'sets total_pages based on total_count and per_page' do
71
- values = calculate_values(Object.new, :total_count => 50, :per_page => 25)
49
+ values = calculate_values(Object.new, current_page: 1, :total_count => 50, :per_page => 25)
72
50
  values[:total_pages].should == 2
73
51
  end
74
52
 
75
- context 'when current_page is not specified' do
76
-
77
- it 'set current_page to the default of 1' do
78
-
79
- values = calculate_values(Object.new)
80
- values[:current_page].should == 1
81
-
82
- end
83
-
84
- end
85
-
86
53
  context 'when current_page is specified' do
87
54
 
88
55
  context 'and the current_page value is <= num_pages'do
@@ -108,6 +75,7 @@ describe 'KaminariRspec::TestHelpers' do
108
75
  end
109
76
 
110
77
  end
78
+
111
79
  end
112
80
 
113
81
 
@@ -6,6 +6,10 @@ require 'rspec'
6
6
  Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
7
7
 
8
8
  RSpec.configure do |config|
9
- config.color_enabled = true
10
- config.formatter = 'documentation'
9
+ config.color = true
10
+ config.formatter = 'documentation'
11
+
12
+ config.expect_with :rspec do |c|
13
+ c.syntax = [:should, :expect]
14
+ end
11
15
  end
metadata CHANGED
@@ -1,111 +1,111 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kaminari-rspec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.14.1
4
+ version: 0.16.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marco Sandrini
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-18 00:00:00.000000000 Z
11
+ date: 2014-12-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: kaminari
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '='
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.14.1
19
+ version: '0.16'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '='
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 0.14.1
26
+ version: '0.16'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rspec
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ! '>='
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: '3'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ! '>='
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: '3'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ! '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ! '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: yard
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ! '>='
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ! '>='
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rr
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ! '>='
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ! '>='
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: mocha
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - ! '>='
87
+ - - ">="
88
88
  - !ruby/object:Gem::Version
89
89
  version: '0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - ! '>='
94
+ - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: flexmock
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - ! '>='
101
+ - - ">="
102
102
  - !ruby/object:Gem::Version
103
103
  version: '0'
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
- - - ! '>='
108
+ - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
111
  description: A Rspec Helper library for the Kaminari gem
@@ -115,8 +115,8 @@ executables: []
115
115
  extensions: []
116
116
  extra_rdoc_files: []
117
117
  files:
118
- - .gitignore
119
- - .travis.yml
118
+ - ".gitignore"
119
+ - ".travis.yml"
120
120
  - Gemfile
121
121
  - License.txt
122
122
  - README.md
@@ -145,17 +145,17 @@ require_paths:
145
145
  - lib
146
146
  required_ruby_version: !ruby/object:Gem::Requirement
147
147
  requirements:
148
- - - ! '>='
148
+ - - ">="
149
149
  - !ruby/object:Gem::Version
150
150
  version: '0'
151
151
  required_rubygems_version: !ruby/object:Gem::Requirement
152
152
  requirements:
153
- - - ! '>='
153
+ - - ">="
154
154
  - !ruby/object:Gem::Version
155
155
  version: '0'
156
156
  requirements: []
157
157
  rubyforge_project:
158
- rubygems_version: 2.0.5
158
+ rubygems_version: 2.2.2
159
159
  signing_key:
160
160
  specification_version: 4
161
161
  summary: A Rspec Helper library for the Kaminari gem