cancan-unit-test 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e9e2b945553ad617f4b57370cc34859783e41a6c
4
- data.tar.gz: de87f9780b9251614cf22f4d1fa243d1078405a6
3
+ metadata.gz: 152e62cc388cf4a4ac5318cf460adf7f6f2bd725
4
+ data.tar.gz: 5bbe663f37bc9787002e0ccfb1b42c66f1c2f9c9
5
5
  SHA512:
6
- metadata.gz: cf9925e1106e20ee585d78e8ecbc0ac83bd59e5c630bce327d797133c16f36e15f15d1f1086c4072142bd1529d454f13ae5246e1e64f28788264725fa3dc25ab
7
- data.tar.gz: 730fa71ca846465dfacf7e01b14ec6e024bfdf05419e33d4eebe086d3cce88e35dfaa540f5055c23aeeebc31c80cc1ec54c35a531048d847fe2e3fabd57b362d
6
+ metadata.gz: 5ad368e034e59e0f45df4c3206081c002120b619478067d1d92f7d1bd8ac48d563249e052b52ab7671e4488c8e26b67c9c3de98641998f41dabb3f363e72ffc7
7
+ data.tar.gz: ed0744b2111cefbaba0f5e6ab24302873609bb04236472cd2005a4faf4d148e4a0c541886b36728c09dc3a6fbefe80705fd3f9c18fd95de761f493a870ff101e
data/.pairs CHANGED
@@ -1,18 +1,26 @@
1
1
  pairs:
2
- co: Cathy O'Connell; cathy
2
+ dmi: Devon Isakow; devon
3
3
  ck: Chien Kuo; ckuo
4
- ra: Rasheed Abdul-Aziz; rabdulaziz
5
4
  rm: Ryan McGarvey; ryan
6
5
  mf: Michael Frederick; mike
7
6
  tm: Todd Mohney; todd
8
7
  my: Micah Young; myoung
9
8
  ba: Alex Babkin; ababkin
10
9
  rn: Ryan Nash; rnash
11
- dmc: Dennis Cahillane; dcahillane
12
10
  jl: Joe Letizia; joe
13
11
  jlm: Joe Moore; joemoore
14
- dc: Dan Connor; dconner
15
12
  lo: Lee Ourand; lourand
13
+ ps: Peter Swan; pdswan
14
+ mc: Michael Chinigo; mchinigo
15
+ int: interview; interview
16
+ dt: David Tengdin
17
+ jp: Joseph Pirtle
18
+ ao: Adekunle Oduye
19
+ kg: Kellye Greene
20
+ mc2: Micah Corn
21
+ ms: Matt Salerno
22
+ fb: Francesco Bertocci
23
+ co: Cathy O
16
24
  email:
17
25
  prefix: zephyr-eng
18
26
  domain: gust.com
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cancan-unit-test (0.0.3)
4
+ cancan-unit-test (0.0.4)
5
5
  activesupport
6
6
  cancan
7
7
 
@@ -21,7 +21,20 @@ describe PostsController do
21
21
  end
22
22
  end
23
23
 
24
- context "stubbing with incorrect matchers" do
24
+ context "stub load resource with a singleton" do
25
+ let_double(:post)
26
+
27
+ before do
28
+ stub_load_singleton_resource(:post) { post }
29
+ end
30
+
31
+ it "uses the stub" do
32
+ get :index
33
+ assigns(:post).should == post
34
+ end
35
+ end
36
+
37
+ context "stubbing with load_and_authorize_resource incorrect matchers" do
25
38
  let_double(:article)
26
39
 
27
40
  before do
@@ -42,4 +55,26 @@ describe PostsController do
42
55
  end
43
56
  end
44
57
  end
58
+
59
+ context "stubbing load resource with incorrect matchers" do
60
+ let_double(:post)
61
+
62
+ before do
63
+ stub_load_singleton_resource(:author, instance_method: :i_am_not_right) { post }
64
+ end
65
+
66
+ context "and warnings turned on", cancan_unit_test_warning: true do
67
+ it "warns that there was no stub found" do
68
+ STDOUT.should_receive(:puts).with("\e[33mCancanUnitTest Warning:\e[0m no stub found for 'load_resource :post'")
69
+ get :index
70
+ end
71
+ end
72
+
73
+ context "and warnings defaulting to off" do
74
+ it "warns that there was no stub found" do
75
+ STDOUT.should_not_receive(:puts).with("\e[33mCancanUnitTest Warning:\e[0m no stub found for 'load_resource :post'")
76
+ get :index
77
+ end
78
+ end
79
+ end
45
80
  end
@@ -1,8 +1,11 @@
1
1
  class PostsController < ApplicationController
2
- load_and_authorize_resource :post
2
+ load_and_authorize_resource :post, except: [:index]
3
+ load_resource :post, only: [:index]
3
4
 
4
5
  def create
5
6
  render :index
6
7
  end
7
8
 
9
+ def index
10
+ end
8
11
  end
@@ -16,8 +16,9 @@ module CancanUnitTest
16
16
 
17
17
  def _shim_load_and_authorize_resource
18
18
  model_name = resource_class.model_name.underscore
19
+ method_name = :load_and_authorize_resource
19
20
 
20
- stub_finder = StubFinder.new(@controller, :load_and_authorize_resource)
21
+ stub_finder = StubFinder.new(@controller, method_name)
21
22
 
22
23
  singleton_stub = stub_finder.find_by_singleton(model_name.to_sym, @options)
23
24
  collection_stub = stub_finder.find_by_collection(model_name.to_sym, @options)
@@ -26,20 +27,40 @@ module CancanUnitTest
26
27
  self.resource_instance = singleton_stub.call if singleton_stub
27
28
  self.collection_instance = collection_stub.call if collection_stub
28
29
  else
29
- warn_about_missing_stub(model_name) if ControllerResource.show_warnings
30
+ warn_about_missing_stub(model_name, method_name) if ControllerResource.show_warnings
30
31
  _original_load_and_authorize_resource
31
32
  end
32
33
  end
33
34
 
35
+ def _shim_load_resource
36
+ model_name = resource_class.model_name.underscore
37
+ method_name = :load_resource
38
+
39
+ stub_finder = StubFinder.new(@controller, method_name)
40
+
41
+ singleton_stub = stub_finder.find_by_singleton(model_name.to_sym, @options)
42
+
43
+ if singleton_stub
44
+ self.resource_instance = singleton_stub.call if singleton_stub
45
+ else
46
+ warn_about_missing_stub(model_name, method_name) if ControllerResource.show_warnings
47
+ _original_load_resource
48
+ end
49
+ end
50
+
34
51
  private
35
52
 
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}'")
53
+ def warn_about_missing_stub(model_name, method)
54
+ puts("\e[33mCancanUnitTest Warning:\e[0m no stub found for '#{method} :#{model_name}'")
55
+ ControllerResource.show_warnings = false
38
56
  end
39
57
 
40
58
  included do
41
59
  alias_method :_original_load_and_authorize_resource, :load_and_authorize_resource
42
60
  alias_method :load_and_authorize_resource, :_shim_load_and_authorize_resource
61
+
62
+ alias_method :_original_load_resource, :load_resource
63
+ alias_method :load_resource, :_shim_load_resource
43
64
  end
44
65
  end
45
66
  end
@@ -9,6 +9,10 @@ module CancanUnitTest
9
9
  controller._add_cancan_unit_test_stub(:load_and_authorize_resource, :collection, model, options, &block)
10
10
  end
11
11
 
12
+ def stub_load_singleton_resource(model, options={}, &block)
13
+ controller._add_cancan_unit_test_stub(:load_resource, :singleton, model, options, &block)
14
+ end
15
+
12
16
  RSpec.configure do |config|
13
17
  config.before(:each, cancan_unit_test_warning: true) do
14
18
  CancanUnitTest::CanCan::ControllerResource.show_warnings = true
@@ -1,3 +1,3 @@
1
1
  module CancanUnitTest
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -19,6 +19,10 @@ module CancanUnitTest
19
19
  raise "original called"
20
20
  end
21
21
 
22
+ def load_resource
23
+ raise "original called"
24
+ end
25
+
22
26
  def resource_class
23
27
  OpenStruct.new({ model_name: @model_name })
24
28
  end
@@ -105,6 +109,69 @@ module CancanUnitTest
105
109
  end
106
110
  end
107
111
  end
112
+
113
+ describe "#load_resource" do
114
+
115
+ let(:controller_resource) { TestControllerResource.new(model_name, options, controller) }
116
+ let(:model_name) { "TheModelName" }
117
+ let_double(:controller)
118
+ let_double(:options)
119
+
120
+ let_double(:block_result)
121
+ let(:block) { double(:block, call: block_result) }
122
+
123
+ let_double(:stub_finder)
124
+
125
+ before do
126
+ StubFinder.stub(:new).with(controller, :load_resource) { stub_finder }
127
+ stub_finder.stub(:find_by_singleton).with(:the_model_name, options) { singleton_results }
128
+ end
129
+
130
+ context "when a stub doesn't exist for the resource" do
131
+ let(:singleton_results) { nil }
132
+
133
+ it "calls through to the original method" do
134
+ expect { controller_resource.load_resource }.to raise_error "original called"
135
+ end
136
+
137
+ context "when showing warning" do
138
+ it "does not warn that there was no stub found" do
139
+ ControllerResource.show_warnings = true
140
+ STDOUT.should_receive(:puts).with("\e[33mCancanUnitTest Warning:\e[0m no stub found for 'load_resource :the_model_name'")
141
+ begin
142
+ controller_resource.load_resource
143
+ rescue
144
+ end
145
+ end
146
+ end
147
+
148
+ context "when suppressing warning" do
149
+ it "warns that there was no stub found" do
150
+ ControllerResource.show_warnings = false
151
+ STDOUT.should_not_receive(:puts).with("\e[33mCancanUnitTest Warning:\e[0m no stub found for 'load_resource :the_model_name'")
152
+ begin
153
+ controller_resource.load_resource
154
+ rescue
155
+ end
156
+ end
157
+ end
158
+ end
159
+
160
+ context "when a singleton stub for the resource exists" do
161
+ let(:singleton_results) { block }
162
+ let(:collection_results) { nil }
163
+
164
+ it "calls the stub" do
165
+ block.should_receive(:call)
166
+ controller_resource.load_resource
167
+ end
168
+
169
+ it "assigns the stub to resource_instance" do
170
+ controller_resource.should_receive(:resource_instance=).with(block_result)
171
+ controller_resource.load_resource
172
+ end
173
+ end
174
+ end
108
175
  end
109
176
  end
110
177
  end
@@ -47,6 +47,16 @@ module CancanUnitTest
47
47
  stub_load_and_authorize_collection_resource(:model, options, &block)
48
48
  end
49
49
  end
50
+
51
+ describe "#stub_load_singleton_resource" do
52
+ it "adds the stubbed singleton resource to the controller" do
53
+ controller.should_receive(:_add_cancan_unit_test_stub).
54
+ with(:load_resource, :singleton, :model, options, &block)
55
+
56
+ rspec_test.
57
+ stub_load_singleton_resource(:model, options, &block)
58
+ end
59
+ end
50
60
  end
51
61
  end
52
62
 
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.3
4
+ version: 0.0.4
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-16 00:00:00.000000000 Z
11
+ date: 2014-03-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cancan