cancan-unit-test 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/cancan_unit_test.gemspec +1 -1
- data/integration/controllers/comments_controller_spec.rb +0 -2
- data/integration/fixtures/dummy/app/controllers/comments_controller.rb +0 -1
- data/lib/cancan_unit_test/action_controller/stub_registry.rb +2 -2
- data/lib/cancan_unit_test/cancan/controller_resource.rb +7 -9
- data/lib/cancan_unit_test/stub_finder.rb +9 -9
- data/lib/cancan_unit_test/version.rb +1 -1
- data/spec/cancan/controller_resource_spec.rb +18 -32
- data/spec/rails/action_controller/stub_registry_spec.rb +13 -14
- data/spec/stub_finder_spec.rb +15 -15
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e9eac8ccda8627bf87ea9a3300901414fac3806c
|
4
|
+
data.tar.gz: 727e13fa64a7f78ccca27ea3c62f7af510ba3a76
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 22bd573b90db60fe476fa4546e3bd812629cf547555310bed7f082d56a05400b5ada2e4e06b16cf32fa07cc9568c468c4135ed943d0df4466aee74d76126c04e
|
7
|
+
data.tar.gz: 9924637bb57e3054df0c80a796104bc060a9400e675a37304705ee1be9c1ac3e9028762ea8d9708d6fcb0b3832d533bc49098f43c321428e3f7217dd04ae01c8
|
data/Gemfile.lock
CHANGED
data/cancan_unit_test.gemspec
CHANGED
@@ -6,7 +6,7 @@ require 'cancan_unit_test/version'
|
|
6
6
|
Gem::Specification.new do |gem|
|
7
7
|
gem.name = "cancan-unit-test"
|
8
8
|
gem.version = CancanUnitTest::VERSION
|
9
|
-
gem.authors = ["Todd Mohney, Rasheed Abdul-Aziz"]
|
9
|
+
gem.authors = ["Todd Mohney, Rasheed Abdul-Aziz, Alex Babkin, Peter Swan"]
|
10
10
|
gem.email = ["zephyr-dev@googlegroups.com"]
|
11
11
|
gem.description = %q{Unit test helpers for CanCan}
|
12
12
|
gem.summary = %q{Unit test helpers for CanCan}
|
@@ -2,9 +2,9 @@ module CancanUnitTest
|
|
2
2
|
module ActionController
|
3
3
|
module StubRegistry
|
4
4
|
|
5
|
-
def _add_cancan_unit_test_stub(method, resource_type,
|
5
|
+
def _add_cancan_unit_test_stub(method, resource_type, resource, options, &block)
|
6
6
|
method_list = _get_cancan_unit_test_stubs(method)
|
7
|
-
method_list << { resource_type: resource_type,
|
7
|
+
method_list << { resource_type: resource_type, resource: resource, options: options, block: block }
|
8
8
|
end
|
9
9
|
|
10
10
|
def _get_cancan_unit_test_stubs(method)
|
@@ -15,43 +15,41 @@ module CancanUnitTest
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def _shim_load_and_authorize_resource
|
18
|
-
model_name = resource_class.model_name.underscore
|
19
18
|
method_name = :load_and_authorize_resource
|
20
19
|
|
21
20
|
stub_finder = StubFinder.new(@controller, method_name)
|
22
21
|
|
23
|
-
singleton_stub = stub_finder.find_by_singleton(
|
24
|
-
collection_stub = stub_finder.find_by_collection(
|
22
|
+
singleton_stub = stub_finder.find_by_singleton(name.to_sym, @options)
|
23
|
+
collection_stub = stub_finder.find_by_collection(name.to_sym, @options)
|
25
24
|
|
26
25
|
if (singleton_stub || collection_stub)
|
27
26
|
self.resource_instance = singleton_stub.call if singleton_stub
|
28
27
|
self.collection_instance = collection_stub.call if collection_stub
|
29
28
|
else
|
30
|
-
warn_about_missing_stub(
|
29
|
+
warn_about_missing_stub(name.to_sym, method_name) if ControllerResource.show_warnings
|
31
30
|
_original_load_and_authorize_resource
|
32
31
|
end
|
33
32
|
end
|
34
33
|
|
35
34
|
def _shim_load_resource
|
36
|
-
model_name = resource_class.model_name.underscore
|
37
35
|
method_name = :load_resource
|
38
36
|
|
39
37
|
stub_finder = StubFinder.new(@controller, method_name)
|
40
38
|
|
41
|
-
singleton_stub = stub_finder.find_by_singleton(
|
39
|
+
singleton_stub = stub_finder.find_by_singleton(name.to_sym, @options)
|
42
40
|
|
43
41
|
if singleton_stub
|
44
42
|
self.resource_instance = singleton_stub.call if singleton_stub
|
45
43
|
else
|
46
|
-
warn_about_missing_stub(
|
44
|
+
warn_about_missing_stub(name.to_sym, method_name) if ControllerResource.show_warnings
|
47
45
|
_original_load_resource
|
48
46
|
end
|
49
47
|
end
|
50
48
|
|
51
49
|
private
|
52
50
|
|
53
|
-
def warn_about_missing_stub(
|
54
|
-
puts("\e[33mCancanUnitTest Warning:\e[0m no stub found for '#{method} :#{
|
51
|
+
def warn_about_missing_stub(name, method)
|
52
|
+
puts("\e[33mCancanUnitTest Warning:\e[0m no stub found for '#{method} :#{name}'")
|
55
53
|
ControllerResource.show_warnings = false
|
56
54
|
end
|
57
55
|
|
@@ -5,18 +5,18 @@ module CancanUnitTest
|
|
5
5
|
@stub_list = controller._get_cancan_unit_test_stubs(method)
|
6
6
|
end
|
7
7
|
|
8
|
-
def find_by_singleton(
|
9
|
-
find_by_resource_type(
|
8
|
+
def find_by_singleton(resource, options)
|
9
|
+
find_by_resource_type(resource, :singleton, options)
|
10
10
|
end
|
11
11
|
|
12
|
-
def find_by_collection(
|
13
|
-
find_by_resource_type(
|
12
|
+
def find_by_collection(resource, options)
|
13
|
+
find_by_resource_type(resource, :collection, options)
|
14
14
|
end
|
15
15
|
|
16
16
|
private
|
17
17
|
|
18
|
-
def find_by_resource_type(
|
19
|
-
filtered_stub_list = filter_stub_list(
|
18
|
+
def find_by_resource_type(resource, resource_type, options)
|
19
|
+
filtered_stub_list = filter_stub_list(resource, resource_type, options)
|
20
20
|
|
21
21
|
return nil if filtered_stub_list.empty?
|
22
22
|
|
@@ -25,11 +25,11 @@ module CancanUnitTest
|
|
25
25
|
filtered_stub_list.first[:block]
|
26
26
|
end
|
27
27
|
|
28
|
-
def filter_stub_list(
|
28
|
+
def filter_stub_list(resource, resource_type, options)
|
29
29
|
stub_list.select do |stub|
|
30
30
|
stub[:resource_type] == resource_type &&
|
31
|
-
stub[:
|
32
|
-
stub[:options] == options
|
31
|
+
stub[:resource] == resource &&
|
32
|
+
stub[:options] == options
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
@@ -3,11 +3,10 @@ require 'spec_helper'
|
|
3
3
|
module CancanUnitTest
|
4
4
|
module CanCan
|
5
5
|
describe ControllerResource do
|
6
|
-
|
7
6
|
class TestControllerResource
|
8
|
-
def initialize(
|
7
|
+
def initialize(name, options, controller)
|
9
8
|
@options = options
|
10
|
-
@
|
9
|
+
@name = name
|
11
10
|
@controller = controller
|
12
11
|
end
|
13
12
|
|
@@ -23,29 +22,27 @@ module CancanUnitTest
|
|
23
22
|
raise "original called"
|
24
23
|
end
|
25
24
|
|
26
|
-
|
27
|
-
OpenStruct.new({ model_name: @model_name })
|
28
|
-
end
|
25
|
+
attr_reader :name
|
29
26
|
|
30
27
|
include CancanUnitTest::CanCan::ControllerResource
|
31
28
|
end
|
32
29
|
|
33
|
-
|
30
|
+
let(:name) { :the_name }
|
31
|
+
let(:controller_resource) { TestControllerResource.new(name, options, controller) }
|
34
32
|
|
35
|
-
|
36
|
-
|
37
|
-
let_double(:controller)
|
38
|
-
let_double(:options)
|
33
|
+
let_double(:controller)
|
34
|
+
let_double(:options)
|
39
35
|
|
40
|
-
|
41
|
-
|
36
|
+
let_double(:block_result)
|
37
|
+
let(:block) { double(:block, call: block_result) }
|
42
38
|
|
43
|
-
|
39
|
+
let_double(:stub_finder)
|
44
40
|
|
41
|
+
describe "#load_and_authorize_resource" do
|
45
42
|
before do
|
46
43
|
StubFinder.stub(:new).with(controller, :load_and_authorize_resource) { stub_finder }
|
47
|
-
stub_finder.stub(:find_by_singleton).with(:
|
48
|
-
stub_finder.stub(:find_by_collection).with(:
|
44
|
+
stub_finder.stub(:find_by_singleton).with(:the_name, options) { singleton_results }
|
45
|
+
stub_finder.stub(:find_by_collection).with(:the_name, options) { collection_results }
|
49
46
|
end
|
50
47
|
|
51
48
|
context "when a stub doesn't exist for the resource" do
|
@@ -59,7 +56,7 @@ module CancanUnitTest
|
|
59
56
|
context "when showing warning" do
|
60
57
|
it "does not warn that there was no stub found" do
|
61
58
|
ControllerResource.show_warnings = true
|
62
|
-
STDOUT.should_receive(:puts).with("\e[33mCancanUnitTest Warning:\e[0m no stub found for 'load_and_authorize_resource :
|
59
|
+
STDOUT.should_receive(:puts).with("\e[33mCancanUnitTest Warning:\e[0m no stub found for 'load_and_authorize_resource :the_name'")
|
63
60
|
begin
|
64
61
|
controller_resource.load_and_authorize_resource
|
65
62
|
rescue
|
@@ -70,7 +67,7 @@ module CancanUnitTest
|
|
70
67
|
context "when suppressing warning" do
|
71
68
|
it "warns that there was no stub found" do
|
72
69
|
ControllerResource.show_warnings = false
|
73
|
-
STDOUT.should_not_receive(:puts).with("\e[33mCancanUnitTest Warning:\e[0m no stub found for 'load_and_authorize_resource :
|
70
|
+
STDOUT.should_not_receive(:puts).with("\e[33mCancanUnitTest Warning:\e[0m no stub found for 'load_and_authorize_resource :the_name'")
|
74
71
|
begin
|
75
72
|
controller_resource.load_and_authorize_resource
|
76
73
|
rescue
|
@@ -111,20 +108,9 @@ module CancanUnitTest
|
|
111
108
|
end
|
112
109
|
|
113
110
|
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
111
|
before do
|
126
112
|
StubFinder.stub(:new).with(controller, :load_resource) { stub_finder }
|
127
|
-
stub_finder.stub(:find_by_singleton).with(:
|
113
|
+
stub_finder.stub(:find_by_singleton).with(:the_name, options) { singleton_results }
|
128
114
|
end
|
129
115
|
|
130
116
|
context "when a stub doesn't exist for the resource" do
|
@@ -137,7 +123,7 @@ module CancanUnitTest
|
|
137
123
|
context "when showing warning" do
|
138
124
|
it "does not warn that there was no stub found" do
|
139
125
|
ControllerResource.show_warnings = true
|
140
|
-
STDOUT.should_receive(:puts).with("\e[33mCancanUnitTest Warning:\e[0m no stub found for 'load_resource :
|
126
|
+
STDOUT.should_receive(:puts).with("\e[33mCancanUnitTest Warning:\e[0m no stub found for 'load_resource :the_name'")
|
141
127
|
begin
|
142
128
|
controller_resource.load_resource
|
143
129
|
rescue
|
@@ -148,7 +134,7 @@ module CancanUnitTest
|
|
148
134
|
context "when suppressing warning" do
|
149
135
|
it "warns that there was no stub found" do
|
150
136
|
ControllerResource.show_warnings = false
|
151
|
-
STDOUT.should_not_receive(:puts).with("\e[33mCancanUnitTest Warning:\e[0m no stub found for 'load_resource :
|
137
|
+
STDOUT.should_not_receive(:puts).with("\e[33mCancanUnitTest Warning:\e[0m no stub found for 'load_resource :the_name'")
|
152
138
|
begin
|
153
139
|
controller_resource.load_resource
|
154
140
|
rescue
|
@@ -11,14 +11,14 @@ module CancanUnitTest
|
|
11
11
|
|
12
12
|
subject(:test_controller) { TestController.new }
|
13
13
|
|
14
|
-
let(:
|
14
|
+
let(:resource) { :some_resource }
|
15
15
|
let(:options) { double(:options) }
|
16
16
|
let(:block) { ->{} }
|
17
17
|
|
18
18
|
let(:expected_singleton_definition) do
|
19
19
|
{
|
20
20
|
resource_type: :singleton,
|
21
|
-
|
21
|
+
resource: resource,
|
22
22
|
options: options,
|
23
23
|
block: block
|
24
24
|
}
|
@@ -27,7 +27,7 @@ module CancanUnitTest
|
|
27
27
|
let(:expected_collection_definition) do
|
28
28
|
{
|
29
29
|
resource_type: :collection,
|
30
|
-
|
30
|
+
resource: resource,
|
31
31
|
options: options,
|
32
32
|
block: block
|
33
33
|
}
|
@@ -45,26 +45,26 @@ module CancanUnitTest
|
|
45
45
|
|
46
46
|
context "added one stub" do
|
47
47
|
before do
|
48
|
-
test_controller._add_cancan_unit_test_stub(:falaffel, :singleton,
|
48
|
+
test_controller._add_cancan_unit_test_stub(:falaffel, :singleton, resource, options, &block)
|
49
49
|
end
|
50
50
|
|
51
51
|
it "should be possible to find the stub with a matching key" do
|
52
52
|
test_controller._get_cancan_unit_test_stubs(:falaffel).should == [expected_singleton_definition]
|
53
53
|
end
|
54
54
|
|
55
|
-
context "adding another with the same
|
55
|
+
context "adding another with the same resource name" do
|
56
56
|
let(:another_singleton_definition) do
|
57
57
|
{
|
58
58
|
resource_type: :singleton,
|
59
|
-
|
60
|
-
|
61
|
-
|
59
|
+
resource: resource,
|
60
|
+
options: options,
|
61
|
+
block: block
|
62
62
|
}
|
63
63
|
end
|
64
64
|
|
65
65
|
context "and the same resource type" do
|
66
66
|
before do
|
67
|
-
test_controller._add_cancan_unit_test_stub(:falaffel, :singleton,
|
67
|
+
test_controller._add_cancan_unit_test_stub(:falaffel, :singleton, resource, options, &block)
|
68
68
|
end
|
69
69
|
|
70
70
|
it "returns both stubs" do
|
@@ -76,7 +76,7 @@ module CancanUnitTest
|
|
76
76
|
|
77
77
|
context "and a different resource type" do
|
78
78
|
before do
|
79
|
-
test_controller._add_cancan_unit_test_stub(:falaffel, :collection,
|
79
|
+
test_controller._add_cancan_unit_test_stub(:falaffel, :collection, resource, options, &block)
|
80
80
|
end
|
81
81
|
|
82
82
|
it "returns both stubs" do
|
@@ -87,20 +87,19 @@ module CancanUnitTest
|
|
87
87
|
end
|
88
88
|
end
|
89
89
|
|
90
|
-
|
91
|
-
context "adding another with a different model name" do
|
90
|
+
context "adding another with a different resource name" do
|
92
91
|
let(:waffle_options) { { syrup: :maple } }
|
93
92
|
let(:waffle_stub_definition) do
|
94
93
|
{
|
95
94
|
resource_type: :singleton,
|
96
|
-
|
95
|
+
resource: resource,
|
97
96
|
options: waffle_options,
|
98
97
|
block: block
|
99
98
|
}
|
100
99
|
end
|
101
100
|
|
102
101
|
before do
|
103
|
-
test_controller._add_cancan_unit_test_stub(:waffle, :singleton,
|
102
|
+
test_controller._add_cancan_unit_test_stub(:waffle, :singleton, resource, waffle_options, &block)
|
104
103
|
end
|
105
104
|
|
106
105
|
it "return the other definition" do
|
data/spec/stub_finder_spec.rb
CHANGED
@@ -4,14 +4,14 @@ module CancanUnitTest
|
|
4
4
|
describe StubFinder do
|
5
5
|
let_double(:controller)
|
6
6
|
let_double(:method)
|
7
|
-
let_double(:
|
7
|
+
let_double(:resource)
|
8
8
|
let_double(:options)
|
9
9
|
let_double(:block)
|
10
10
|
|
11
11
|
let(:finder) { StubFinder.new(controller, method) }
|
12
12
|
|
13
13
|
describe "#find_by_singleton" do
|
14
|
-
subject { finder.find_by_singleton(
|
14
|
+
subject { finder.find_by_singleton(resource, options) }
|
15
15
|
|
16
16
|
before do
|
17
17
|
controller.
|
@@ -26,18 +26,18 @@ module CancanUnitTest
|
|
26
26
|
end
|
27
27
|
|
28
28
|
context "the controller has a stub for the method" do
|
29
|
-
context "with no matching
|
30
|
-
let(:results) { [{ resource_type: :singleton,
|
29
|
+
context "with no matching resource" do
|
30
|
+
let(:results) { [{ resource_type: :singleton, resource: double(:other_resource), options: options, block: block }] }
|
31
31
|
it { should be_nil }
|
32
32
|
end
|
33
33
|
|
34
34
|
context "with no matching options" do
|
35
|
-
let(:results) { [{ resource_type: :singleton,
|
35
|
+
let(:results) { [{ resource_type: :singleton, resource: resource, options: double(:other_options), block: block }] }
|
36
36
|
it { should be_nil }
|
37
37
|
end
|
38
38
|
|
39
|
-
context "with matching
|
40
|
-
let(:results) { [{ resource_type: :singleton,
|
39
|
+
context "with matching resource and options" do
|
40
|
+
let(:results) { [{ resource_type: :singleton, resource: resource, options: options, block: block }] }
|
41
41
|
it { should == block }
|
42
42
|
end
|
43
43
|
end
|
@@ -46,8 +46,8 @@ module CancanUnitTest
|
|
46
46
|
context "with a different resource type" do
|
47
47
|
let(:results) do
|
48
48
|
[
|
49
|
-
{ resource_type: :collection,
|
50
|
-
{ resource_type: :singleton,
|
49
|
+
{ resource_type: :collection, resource: resource, options: options, block: -> {} },
|
50
|
+
{ resource_type: :singleton, resource: resource, options: options, block: block }
|
51
51
|
]
|
52
52
|
end
|
53
53
|
|
@@ -57,13 +57,13 @@ module CancanUnitTest
|
|
57
57
|
context "with the same resource type" do
|
58
58
|
let(:results) do
|
59
59
|
[
|
60
|
-
{ resource_type: :singleton,
|
61
|
-
{ resource_type: :singleton,
|
60
|
+
{ resource_type: :singleton, resource: resource, options: options, block: block },
|
61
|
+
{ resource_type: :singleton, resource: resource, options: options, block: block }
|
62
62
|
]
|
63
63
|
end
|
64
64
|
|
65
65
|
it "raises an exception" do
|
66
|
-
expect { finder.find_by_singleton(
|
66
|
+
expect { finder.find_by_singleton(resource, options) }.to raise_error
|
67
67
|
end
|
68
68
|
end
|
69
69
|
end
|
@@ -71,7 +71,7 @@ module CancanUnitTest
|
|
71
71
|
end
|
72
72
|
|
73
73
|
describe "#find_by_collection" do
|
74
|
-
subject { finder.find_by_collection(
|
74
|
+
subject { finder.find_by_collection(resource, options) }
|
75
75
|
|
76
76
|
before do
|
77
77
|
controller.
|
@@ -83,8 +83,8 @@ module CancanUnitTest
|
|
83
83
|
context "with a different resource type" do
|
84
84
|
let(:results) do
|
85
85
|
[
|
86
|
-
{ resource_type: :singleton,
|
87
|
-
{ resource_type: :collection,
|
86
|
+
{ resource_type: :singleton, resource: resource, options: options, block: -> {} },
|
87
|
+
{ resource_type: :collection, resource: resource, options: options, block: block }
|
88
88
|
]
|
89
89
|
end
|
90
90
|
|
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.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
- Todd Mohney, Rasheed Abdul-Aziz
|
7
|
+
- Todd Mohney, Rasheed Abdul-Aziz, Alex Babkin, Peter Swan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-07-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: cancan
|
@@ -237,7 +237,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
237
237
|
version: '0'
|
238
238
|
requirements: []
|
239
239
|
rubyforge_project:
|
240
|
-
rubygems_version: 2.
|
240
|
+
rubygems_version: 2.1.11
|
241
241
|
signing_key:
|
242
242
|
specification_version: 4
|
243
243
|
summary: Unit test helpers for CanCan
|