cancan-unit-test 0.0.2 → 0.0.3

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,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aa942f63f5ef9c7e8d0e568d49fe7a6bc2781c14
4
- data.tar.gz: e2814f79002bf4a8594ad1632fa2124bb1b970a6
3
+ metadata.gz: e9e2b945553ad617f4b57370cc34859783e41a6c
4
+ data.tar.gz: de87f9780b9251614cf22f4d1fa243d1078405a6
5
5
  SHA512:
6
- metadata.gz: abd119ec9e7292d3f1885c486422b667bbc5fb7ba4705695737c68e3537be602e429820f598b39430ebba12e62abb3b29e825f41920037c3759a4c2843ae8508
7
- data.tar.gz: 92bd77b5b3813620e6f0f8fb0fe1157bc97dc94c4f3de8d8b39623db4b661f2b0bf7ea7392b4c57d609be28ac233e177b490a7b239825486a4a5302f2fb5f642
6
+ metadata.gz: cf9925e1106e20ee585d78e8ecbc0ac83bd59e5c630bce327d797133c16f36e15f15d1f1086c4072142bd1529d454f13ae5246e1e64f28788264725fa3dc25ab
7
+ data.tar.gz: 730fa71ca846465dfacf7e01b14ec6e024bfdf05419e33d4eebe086d3cce88e35dfaa540f5055c23aeeebc31c80cc1ec54c35a531048d847fe2e3fabd57b362d
data/.gitignore CHANGED
@@ -17,7 +17,7 @@ tmp
17
17
  integration/fixtures/dummy/log
18
18
  integration/fixtures/dummy/*.gem
19
19
  integration/fixtures/dummy/*.rbc
20
- integration/fixtures/dummy/db/*.sqlite3
20
+ integration/fixtures/dummy/db/development.sqlite3
21
21
  integration/fixtures/dummy/.bundle
22
22
  integration/fixtures/dummy/.config
23
23
  integration/fixtures/dummy/.yardoc
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cancan-unit-test (0.0.2)
4
+ cancan-unit-test (0.0.3)
5
5
  activesupport
6
6
  cancan
7
7
 
@@ -28,9 +28,18 @@ describe PostsController do
28
28
  stub_load_and_authorize_singleton_resource(:post, instance_method: :i_am_not_right) { article }
29
29
  end
30
30
 
31
- it "warns that there was no stub found" do
32
- STDOUT.should_receive(:puts).with("\e[33mCancanUnitTest Warning:\e[0m no stub found for 'load_and_authorize_resource :post'")
33
- post :create
31
+ context "and warnings turned on", cancan_unit_test_warning: true do
32
+ it "warns that there was no stub found" do
33
+ STDOUT.should_receive(:puts).with("\e[33mCancanUnitTest Warning:\e[0m no stub found for 'load_and_authorize_resource :post'")
34
+ post :create
35
+ end
36
+ end
37
+
38
+ context "and warnings defaulting to off" do
39
+ it "warns that there was no stub found" do
40
+ STDOUT.should_not_receive(:puts).with("\e[33mCancanUnitTest Warning:\e[0m no stub found for 'load_and_authorize_resource :post'")
41
+ post :create
42
+ end
34
43
  end
35
44
  end
36
45
  end
@@ -0,0 +1,36 @@
1
+ require 'integration_helper'
2
+
3
+ describe PostsController do
4
+ context "not stubbing" do
5
+ it "uses can can" do
6
+ post :create
7
+ assigns(:post).should be_a Post
8
+ end
9
+ end
10
+
11
+ context "stubbing load and authorize with a singleton" do
12
+ let_double(:article)
13
+
14
+ before do
15
+ stub_load_and_authorize_singleton_resource(:post) { article }
16
+ end
17
+
18
+ it "uses the stub" do
19
+ post :create
20
+ assigns(:post).should == article
21
+ end
22
+ end
23
+
24
+ context "stubbing with incorrect matchers" do
25
+ let_double(:article)
26
+
27
+ before do
28
+ stub_load_and_authorize_singleton_resource(:post, instance_method: :i_am_not_right) { article }
29
+ end
30
+
31
+ it "warns that there was no stub found" do
32
+ STDOUT.should_receive(:puts).with("\e[33mCancanUnitTest Warning:\e[0m no stub found for 'load_and_authorize_resource :post'")
33
+ post :create
34
+ end
35
+ end
36
+ end
@@ -6,6 +6,14 @@ module CancanUnitTest
6
6
  module ControllerResource
7
7
  extend ::ActiveSupport::Concern
8
8
 
9
+ def self.show_warnings= value
10
+ @show_warnings = value
11
+ end
12
+
13
+ def self.show_warnings
14
+ @show_warnings ||= false
15
+ end
16
+
9
17
  def _shim_load_and_authorize_resource
10
18
  model_name = resource_class.model_name.underscore
11
19
 
@@ -18,11 +26,17 @@ module CancanUnitTest
18
26
  self.resource_instance = singleton_stub.call if singleton_stub
19
27
  self.collection_instance = collection_stub.call if collection_stub
20
28
  else
21
- puts("\e[33mCancanUnitTest Warning:\e[0m no stub found for 'load_and_authorize_resource :#{model_name}'")
29
+ warn_about_missing_stub(model_name) if ControllerResource.show_warnings
22
30
  _original_load_and_authorize_resource
23
31
  end
24
32
  end
25
33
 
34
+ private
35
+
36
+ def warn_about_missing_stub(model_name)
37
+ puts("\e[33mCancanUnitTest Warning:\e[0m no stub found for 'load_and_authorize_resource :#{model_name}'")
38
+ end
39
+
26
40
  included do
27
41
  alias_method :_original_load_and_authorize_resource, :load_and_authorize_resource
28
42
  alias_method :load_and_authorize_resource, :_shim_load_and_authorize_resource
@@ -0,0 +1,39 @@
1
+ require 'active_support/concern'
2
+ require 'active_support/inflector'
3
+
4
+ module CancanUnitTest
5
+ module CanCan
6
+ module ControllerResource
7
+ extend ::ActiveSupport::Concern
8
+
9
+ def _shim_load_and_authorize_resource
10
+ model_name = resource_class.model_name.underscore
11
+
12
+ stub_finder = StubFinder.new(@controller, :load_and_authorize_resource)
13
+
14
+ singleton_stub = stub_finder.find_by_singleton(model_name.to_sym, @options)
15
+ collection_stub = stub_finder.find_by_collection(model_name.to_sym, @options)
16
+
17
+ if (singleton_stub || collection_stub)
18
+ self.resource_instance = singleton_stub.call if singleton_stub
19
+ self.collection_instance = collection_stub.call if collection_stub
20
+ else
21
+ puts("\e[33mCancanUnitTest Warning:\e[0m no stub found for 'load_and_authorize_resource :#{model_name}'")
22
+ _original_load_and_authorize_resource
23
+ end
24
+ end
25
+
26
+ included do
27
+ alias_method :_original_load_and_authorize_resource, :load_and_authorize_resource
28
+ alias_method :load_and_authorize_resource, :_shim_load_and_authorize_resource
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+
35
+ if defined? CanCan::ControllerResource
36
+ CanCan::ControllerResource.class_eval do
37
+ include CancanUnitTest::CanCan::ControllerResource
38
+ end
39
+ end
@@ -8,5 +8,16 @@ module CancanUnitTest
8
8
  def stub_load_and_authorize_collection_resource(model, options={}, &block)
9
9
  controller._add_cancan_unit_test_stub(:load_and_authorize_resource, :collection, model, options, &block)
10
10
  end
11
+
12
+ RSpec.configure do |config|
13
+ config.before(:each, cancan_unit_test_warning: true) do
14
+ CancanUnitTest::CanCan::ControllerResource.show_warnings = true
15
+ end
16
+
17
+ config.after(:each, cancan_unit_test_warning: true) do
18
+ CancanUnitTest::CanCan::ControllerResource.show_warnings = false
19
+ end
20
+ end
21
+
11
22
  end
12
23
  end
@@ -0,0 +1,12 @@
1
+ module CancanUnitTest
2
+ module Mocks
3
+
4
+ def stub_load_and_authorize_singleton_resource(model, options={}, &block)
5
+ controller._add_cancan_unit_test_stub(:load_and_authorize_resource, :singleton, model, options, &block)
6
+ end
7
+
8
+ def stub_load_and_authorize_collection_resource(model, options={}, &block)
9
+ controller._add_cancan_unit_test_stub(:load_and_authorize_resource, :collection, model, options, &block)
10
+ end
11
+ end
12
+ end
@@ -1,3 +1,3 @@
1
1
  module CancanUnitTest
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -0,0 +1,3 @@
1
+ module CancanUnitTest
2
+ VERSION = "0.0.2"
3
+ end
@@ -52,11 +52,25 @@ module CancanUnitTest
52
52
  expect { controller_resource.load_and_authorize_resource }.to raise_error "original called"
53
53
  end
54
54
 
55
- it "warns that there was no stub found" do
56
- STDOUT.should_receive(:puts).with("\e[33mCancanUnitTest Warning:\e[0m no stub found for 'load_and_authorize_resource :the_model_name'")
57
- begin
58
- controller_resource.load_and_authorize_resource
59
- rescue
55
+ context "when showing warning" do
56
+ it "does not warn that there was no stub found" do
57
+ ControllerResource.show_warnings = true
58
+ STDOUT.should_receive(:puts).with("\e[33mCancanUnitTest Warning:\e[0m no stub found for 'load_and_authorize_resource :the_model_name'")
59
+ begin
60
+ controller_resource.load_and_authorize_resource
61
+ rescue
62
+ end
63
+ end
64
+ end
65
+
66
+ context "when suppressing warning" do
67
+ it "warns that there was no stub found" do
68
+ ControllerResource.show_warnings = false
69
+ STDOUT.should_not_receive(:puts).with("\e[33mCancanUnitTest Warning:\e[0m no stub found for 'load_and_authorize_resource :the_model_name'")
70
+ begin
71
+ controller_resource.load_and_authorize_resource
72
+ rescue
73
+ end
60
74
  end
61
75
  end
62
76
  end
@@ -0,0 +1,96 @@
1
+ require 'spec_helper'
2
+
3
+ module CancanUnitTest
4
+ module CanCan
5
+ describe ControllerResource do
6
+
7
+ class TestControllerResource
8
+ def initialize(model_name, options, controller)
9
+ @options = options
10
+ @model_name = model_name
11
+ @controller = controller
12
+ end
13
+
14
+ def resource_instance=(value) end
15
+
16
+ def collection_instance=(value) end
17
+
18
+ def load_and_authorize_resource
19
+ raise "original called"
20
+ end
21
+
22
+ def resource_class
23
+ OpenStruct.new({ model_name: @model_name })
24
+ end
25
+
26
+ include CancanUnitTest::CanCan::ControllerResource
27
+ end
28
+
29
+ describe "#load_and_authorize_resource" do
30
+
31
+ let(:controller_resource) { TestControllerResource.new(model_name, options, controller) }
32
+ let(:model_name) { "TheModelName" }
33
+ let_double(:controller)
34
+ let_double(:options)
35
+
36
+ let_double(:block_result)
37
+ let(:block) { double(:block, call: block_result) }
38
+
39
+ let_double(:stub_finder)
40
+
41
+ before do
42
+ StubFinder.stub(:new).with(controller, :load_and_authorize_resource) { stub_finder }
43
+ stub_finder.stub(:find_by_singleton).with(:the_model_name, options) { singleton_results }
44
+ stub_finder.stub(:find_by_collection).with(:the_model_name, options) { collection_results }
45
+ end
46
+
47
+ context "when a stub doesn't exist for the resource" do
48
+ let(:singleton_results) { nil }
49
+ let(:collection_results) { nil }
50
+
51
+ it "calls through to the original method" do
52
+ expect { controller_resource.load_and_authorize_resource }.to raise_error "original called"
53
+ end
54
+
55
+ it "warns that there was no stub found" do
56
+ STDOUT.should_receive(:puts).with("\e[33mCancanUnitTest Warning:\e[0m no stub found for 'load_and_authorize_resource :the_model_name'")
57
+ begin
58
+ controller_resource.load_and_authorize_resource
59
+ rescue
60
+ end
61
+ end
62
+ end
63
+
64
+ context "when a singleton stub for the resource exists" do
65
+ let(:singleton_results) { block }
66
+ let(:collection_results) { nil }
67
+
68
+ it "calls the stub" do
69
+ block.should_receive(:call)
70
+ controller_resource.load_and_authorize_resource
71
+ end
72
+
73
+ it "assigns the stub to resource_instance" do
74
+ controller_resource.should_receive(:resource_instance=).with(block_result)
75
+ controller_resource.load_and_authorize_resource
76
+ end
77
+ end
78
+
79
+ context "when a collection stub for the resource exists" do
80
+ let(:singleton_results) { nil }
81
+ let(:collection_results) { block }
82
+
83
+ it "calls the stub" do
84
+ block.should_receive(:call)
85
+ controller_resource.load_and_authorize_resource
86
+ end
87
+
88
+ it "assigns the stub to collection_instance" do
89
+ controller_resource.should_receive(:collection_instance=).with(block_result)
90
+ controller_resource.load_and_authorize_resource
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cancan-unit-test
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Todd Mohney, Rasheed Abdul-Aziz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-12 00:00:00.000000000 Z
11
+ date: 2013-07-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cancan
@@ -155,6 +155,7 @@ files:
155
155
  - cancan_unit_test.gemspec
156
156
  - integration/controllers/comments_controller_spec.rb
157
157
  - integration/controllers/posts_controller_spec.rb
158
+ - integration/controllers/posts_controller_spec.rb.orig
158
159
  - integration/fixtures/dummy/README.rdoc
159
160
  - integration/fixtures/dummy/Rakefile
160
161
  - integration/fixtures/dummy/app/assets/javascripts/application.js
@@ -191,6 +192,7 @@ files:
191
192
  - integration/fixtures/dummy/db/migrate/20130712144534_create_authors.rb
192
193
  - integration/fixtures/dummy/db/migrate/20130712144551_create_comments.rb
193
194
  - integration/fixtures/dummy/db/schema.rb
195
+ - integration/fixtures/dummy/db/test.sqlite3
194
196
  - integration/fixtures/dummy/lib/assets/.gitkeep
195
197
  - integration/fixtures/dummy/log/.gitkeep
196
198
  - integration/fixtures/dummy/public/404.html
@@ -202,11 +204,15 @@ files:
202
204
  - lib/cancan_unit_test/action_controller/stub_registry.rb
203
205
  - lib/cancan_unit_test/action_controller_extensions.rb
204
206
  - lib/cancan_unit_test/cancan/controller_resource.rb
207
+ - lib/cancan_unit_test/cancan/controller_resource.rb.orig
205
208
  - lib/cancan_unit_test/cancan_extensions.rb
206
209
  - lib/cancan_unit_test/mocks.rb
210
+ - lib/cancan_unit_test/mocks.rb.orig
207
211
  - lib/cancan_unit_test/stub_finder.rb
208
212
  - lib/cancan_unit_test/version.rb
213
+ - lib/cancan_unit_test/version.rb.orig
209
214
  - spec/cancan/controller_resource_spec.rb
215
+ - spec/cancan/controller_resource_spec.rb.orig
210
216
  - spec/integration_helper.rb
211
217
  - spec/mocks_spec.rb
212
218
  - spec/rails/action_controller/stub_registry_spec.rb
@@ -237,6 +243,7 @@ specification_version: 4
237
243
  summary: Unit test helpers for CanCan
238
244
  test_files:
239
245
  - spec/cancan/controller_resource_spec.rb
246
+ - spec/cancan/controller_resource_spec.rb.orig
240
247
  - spec/integration_helper.rb
241
248
  - spec/mocks_spec.rb
242
249
  - spec/rails/action_controller/stub_registry_spec.rb