cancan-unit-test 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/Gemfile.lock +1 -1
- data/README.md +2 -1
- data/integration/controllers/comments_controller_spec.rb +36 -0
- data/integration/controllers/posts_controller_spec.rb +36 -0
- data/integration/fixtures/dummy/app/controllers/comments_controller.rb +11 -0
- data/integration/fixtures/dummy/app/controllers/posts_controller.rb +1 -1
- data/integration/fixtures/dummy/app/models/author.rb +3 -0
- data/integration/fixtures/dummy/app/models/comment.rb +3 -0
- data/integration/fixtures/dummy/config/routes.rb +3 -57
- data/integration/fixtures/dummy/db/migrate/20130709143526_create_posts.rb +1 -0
- data/integration/fixtures/dummy/db/migrate/20130712144534_create_authors.rb +9 -0
- data/integration/fixtures/dummy/db/migrate/20130712144551_create_comments.rb +10 -0
- data/integration/fixtures/dummy/db/schema.rb +15 -1
- data/lib/cancan_unit_test/action_controller/stub_registry.rb +4 -4
- data/lib/cancan_unit_test/cancan/controller_resource.rb +10 -8
- data/lib/cancan_unit_test/mocks.rb +5 -2
- data/lib/cancan_unit_test/stub_finder.rb +19 -10
- data/lib/cancan_unit_test/version.rb +1 -1
- data/spec/cancan/controller_resource_spec.rb +35 -54
- data/spec/mocks_spec.rb +20 -7
- data/spec/rails/action_controller/stub_registry_spec.rb +42 -17
- data/spec/stub_finder_spec.rb +46 -10
- metadata +9 -6
- data/integration/controllers/mocks_spec.rb +0 -25
- data/integration/fixtures/dummy/db/development.sqlite3 +0 -0
- data/integration/fixtures/dummy/db/structure.sql +0 -1
- data/integration/fixtures/dummy/db/test.sqlite3 +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aa942f63f5ef9c7e8d0e568d49fe7a6bc2781c14
|
4
|
+
data.tar.gz: e2814f79002bf4a8594ad1632fa2124bb1b970a6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: abd119ec9e7292d3f1885c486422b667bbc5fb7ba4705695737c68e3537be602e429820f598b39430ebba12e62abb3b29e825f41920037c3759a4c2843ae8508
|
7
|
+
data.tar.gz: 92bd77b5b3813620e6f0f8fb0fe1157bc97dc94c4f3de8d8b39623db4b661f2b0bf7ea7392b4c57d609be28ac233e177b490a7b239825486a4a5302f2fb5f642
|
data/.gitignore
CHANGED
@@ -17,6 +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
21
|
integration/fixtures/dummy/.bundle
|
21
22
|
integration/fixtures/dummy/.config
|
22
23
|
integration/fixtures/dummy/.yardoc
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -20,7 +20,8 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
|
23
|
+
stub_load_and_authorize_singleton_resource(:model_name, options_hash)
|
24
|
+
stub_load_and_authorize_collection_resource(:model_name, options_hash)
|
24
25
|
|
25
26
|
## Contributing
|
26
27
|
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'integration_helper'
|
2
|
+
|
3
|
+
describe CommentsController do
|
4
|
+
|
5
|
+
context "stubbing with incorrect resource instance or collection" do
|
6
|
+
let_double(:post_object)
|
7
|
+
let_double(:post_id)
|
8
|
+
let_double(:post_author)
|
9
|
+
let_double(:comments)
|
10
|
+
|
11
|
+
before do
|
12
|
+
stub_load_and_authorize_singleton_resource(:post) { post_object }
|
13
|
+
stub_load_and_authorize_collection_resource(:comment) { comments }
|
14
|
+
stub_load_and_authorize_singleton_resource(:author, instance_name: :post_author, through: :post, parent: false) { post_author }
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "index" do
|
18
|
+
it "assigns the post" do
|
19
|
+
get :index, post_id: post_id
|
20
|
+
assigns(:post).should == post_object
|
21
|
+
end
|
22
|
+
|
23
|
+
it "assigns the comments" do
|
24
|
+
get :index, post_id: post_id
|
25
|
+
assigns(:comments).should == comments
|
26
|
+
end
|
27
|
+
|
28
|
+
it "assigns the post's author" do
|
29
|
+
get :index, post_id: post_id
|
30
|
+
assigns(:post_author).should == post_author
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
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
|
@@ -1,61 +1,7 @@
|
|
1
1
|
Dummy::Application.routes.draw do
|
2
2
|
|
3
|
-
resources :posts
|
3
|
+
resources :posts do
|
4
|
+
resources :comments
|
5
|
+
end
|
4
6
|
|
5
|
-
# The priority is based upon order of creation:
|
6
|
-
# first created -> highest priority.
|
7
|
-
|
8
|
-
# Sample of regular route:
|
9
|
-
# match 'products/:id' => 'catalog#view'
|
10
|
-
# Keep in mind you can assign values other than :controller and :action
|
11
|
-
|
12
|
-
# Sample of named route:
|
13
|
-
# match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
|
14
|
-
# This route can be invoked with purchase_url(:id => product.id)
|
15
|
-
|
16
|
-
# Sample resource route (maps HTTP verbs to controller actions automatically):
|
17
|
-
# resources :products
|
18
|
-
|
19
|
-
# Sample resource route with options:
|
20
|
-
# resources :products do
|
21
|
-
# member do
|
22
|
-
# get 'short'
|
23
|
-
# post 'toggle'
|
24
|
-
# end
|
25
|
-
#
|
26
|
-
# collection do
|
27
|
-
# get 'sold'
|
28
|
-
# end
|
29
|
-
# end
|
30
|
-
|
31
|
-
# Sample resource route with sub-resources:
|
32
|
-
# resources :products do
|
33
|
-
# resources :comments, :sales
|
34
|
-
# resource :seller
|
35
|
-
# end
|
36
|
-
|
37
|
-
# Sample resource route with more complex sub-resources
|
38
|
-
# resources :products do
|
39
|
-
# resources :comments
|
40
|
-
# resources :sales do
|
41
|
-
# get 'recent', :on => :collection
|
42
|
-
# end
|
43
|
-
# end
|
44
|
-
|
45
|
-
# Sample resource route within a namespace:
|
46
|
-
# namespace :admin do
|
47
|
-
# # Directs /admin/products/* to Admin::ProductsController
|
48
|
-
# # (app/controllers/admin/products_controller.rb)
|
49
|
-
# resources :products
|
50
|
-
# end
|
51
|
-
|
52
|
-
# You can have the root of your site routed with "root"
|
53
|
-
# just remember to delete public/index.html.
|
54
|
-
# root :to => 'welcome#index'
|
55
|
-
|
56
|
-
# See how all your routes lay out with "rake routes"
|
57
|
-
|
58
|
-
# This is a legacy wild controller route that's not recommended for RESTful applications.
|
59
|
-
# Note: This route will make all actions in every controller accessible via GET requests.
|
60
|
-
# match ':controller(/:action(/:id))(.:format)'
|
61
7
|
end
|
@@ -11,9 +11,23 @@
|
|
11
11
|
#
|
12
12
|
# It's strongly recommended to check this file into your version control system.
|
13
13
|
|
14
|
-
ActiveRecord::Schema.define(:version =>
|
14
|
+
ActiveRecord::Schema.define(:version => 20130712144551) do
|
15
|
+
|
16
|
+
create_table "authors", :force => true do |t|
|
17
|
+
t.string "name"
|
18
|
+
t.datetime "created_at", :null => false
|
19
|
+
t.datetime "updated_at", :null => false
|
20
|
+
end
|
21
|
+
|
22
|
+
create_table "comments", :force => true do |t|
|
23
|
+
t.integer "post_id"
|
24
|
+
t.string "content"
|
25
|
+
t.datetime "created_at", :null => false
|
26
|
+
t.datetime "updated_at", :null => false
|
27
|
+
end
|
15
28
|
|
16
29
|
create_table "posts", :force => true do |t|
|
30
|
+
t.integer "author_id"
|
17
31
|
t.string "title"
|
18
32
|
t.text "content"
|
19
33
|
t.datetime "created_at", :null => false
|
@@ -2,13 +2,13 @@ module CancanUnitTest
|
|
2
2
|
module ActionController
|
3
3
|
module StubRegistry
|
4
4
|
|
5
|
-
def _add_cancan_unit_test_stub(method, model, options, &block)
|
6
|
-
method_list =
|
7
|
-
method_list << { model: model, options: options, block: block }
|
5
|
+
def _add_cancan_unit_test_stub(method, resource_type, model, options, &block)
|
6
|
+
method_list = _get_cancan_unit_test_stubs(method)
|
7
|
+
method_list << { resource_type: resource_type, model: model, options: options, block: block }
|
8
8
|
end
|
9
9
|
|
10
10
|
def _get_cancan_unit_test_stubs(method)
|
11
|
-
cancan_unit_test_stubs[method]
|
11
|
+
cancan_unit_test_stubs[method] ||= []
|
12
12
|
end
|
13
13
|
|
14
14
|
private
|
@@ -7,17 +7,19 @@ module CancanUnitTest
|
|
7
7
|
extend ::ActiveSupport::Concern
|
8
8
|
|
9
9
|
def _shim_load_and_authorize_resource
|
10
|
+
model_name = resource_class.model_name.underscore
|
11
|
+
|
10
12
|
stub_finder = StubFinder.new(@controller, :load_and_authorize_resource)
|
11
|
-
stub = stub_finder.find(resource_class.model_name.underscore.to_sym, @options)
|
12
13
|
|
13
|
-
|
14
|
-
|
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
|
15
20
|
else
|
16
|
-
|
17
|
-
|
18
|
-
elsif load_collection?
|
19
|
-
self.collection_instance = stub.call
|
20
|
-
end
|
21
|
+
puts("\e[33mCancanUnitTest Warning:\e[0m no stub found for 'load_and_authorize_resource :#{model_name}'")
|
22
|
+
_original_load_and_authorize_resource
|
21
23
|
end
|
22
24
|
end
|
23
25
|
|
@@ -1,9 +1,12 @@
|
|
1
1
|
module CancanUnitTest
|
2
2
|
module Mocks
|
3
3
|
|
4
|
-
def
|
5
|
-
controller._add_cancan_unit_test_stub(:load_and_authorize_resource, model, options, &block)
|
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
6
|
end
|
7
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
|
8
11
|
end
|
9
12
|
end
|
@@ -5,22 +5,31 @@ module CancanUnitTest
|
|
5
5
|
@stub_list = controller._get_cancan_unit_test_stubs(method)
|
6
6
|
end
|
7
7
|
|
8
|
-
def
|
9
|
-
|
10
|
-
|
11
|
-
return nil if stub_list.empty?
|
12
|
-
|
13
|
-
raise "Ambiguous match in CanCan:Mocks" if stub_list.count > 1
|
8
|
+
def find_by_singleton(model, options)
|
9
|
+
find_by_resource_type(model, :singleton, options)
|
10
|
+
end
|
14
11
|
|
15
|
-
|
12
|
+
def find_by_collection(model, options)
|
13
|
+
find_by_resource_type(model, :collection, options)
|
16
14
|
end
|
17
15
|
|
18
16
|
private
|
19
17
|
|
20
|
-
def
|
18
|
+
def find_by_resource_type(model, resource_type, options)
|
19
|
+
filtered_stub_list = filter_stub_list(model, resource_type, options)
|
20
|
+
|
21
|
+
return nil if filtered_stub_list.empty?
|
22
|
+
|
23
|
+
raise "Ambiguous match in CanCan:Mocks" if filtered_stub_list.count > 1
|
24
|
+
|
25
|
+
filtered_stub_list.first[:block]
|
26
|
+
end
|
27
|
+
|
28
|
+
def filter_stub_list(model, resource_type, options)
|
21
29
|
stub_list.select do |stub|
|
22
|
-
stub[:
|
23
|
-
stub[:
|
30
|
+
stub[:resource_type] == resource_type &&
|
31
|
+
stub[:model] == model &&
|
32
|
+
stub[:options] == options
|
24
33
|
end
|
25
34
|
end
|
26
35
|
|
@@ -5,7 +5,6 @@ module CancanUnitTest
|
|
5
5
|
describe ControllerResource do
|
6
6
|
|
7
7
|
class TestControllerResource
|
8
|
-
|
9
8
|
def initialize(model_name, options, controller)
|
10
9
|
@options = options
|
11
10
|
@model_name = model_name
|
@@ -28,88 +27,70 @@ module CancanUnitTest
|
|
28
27
|
end
|
29
28
|
|
30
29
|
describe "#load_and_authorize_resource" do
|
30
|
+
|
31
31
|
let(:controller_resource) { TestControllerResource.new(model_name, options, controller) }
|
32
|
-
let_double(:stub_finder)
|
33
|
-
let_double(:method)
|
34
32
|
let(:model_name) { "TheModelName" }
|
33
|
+
let_double(:controller)
|
35
34
|
let_double(:options)
|
35
|
+
|
36
36
|
let_double(:block_result)
|
37
|
-
let_double(:controller)
|
38
37
|
let(:block) { double(:block, call: block_result) }
|
39
38
|
|
39
|
+
let_double(:stub_finder)
|
40
|
+
|
40
41
|
before do
|
41
42
|
StubFinder.stub(:new).with(controller, :load_and_authorize_resource) { stub_finder }
|
42
|
-
stub_finder.stub(:
|
43
|
-
|
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 }
|
44
45
|
end
|
45
46
|
|
46
47
|
context "when a stub doesn't exist for the resource" do
|
47
|
-
let(:
|
48
|
-
let(:
|
48
|
+
let(:singleton_results) { nil }
|
49
|
+
let(:collection_results) { nil }
|
49
50
|
|
50
51
|
it "calls through to the original method" do
|
51
52
|
expect { controller_resource.load_and_authorize_resource }.to raise_error "original called"
|
52
53
|
end
|
53
|
-
end
|
54
|
-
|
55
|
-
context "when a stub exists for the resource" do
|
56
|
-
let(:stub_finder_results) { block }
|
57
|
-
let(:load_instance) { true }
|
58
|
-
|
59
|
-
context "when loading a single resource instance" do
|
60
|
-
let(:load_instance) { true }
|
61
|
-
|
62
|
-
it "calls the stub" do
|
63
|
-
block.should_receive(:call)
|
64
|
-
controller_resource.load_and_authorize_resource
|
65
|
-
end
|
66
54
|
|
67
|
-
|
68
|
-
|
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
|
69
58
|
controller_resource.load_and_authorize_resource
|
59
|
+
rescue
|
70
60
|
end
|
71
61
|
end
|
62
|
+
end
|
72
63
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
before do
|
77
|
-
controller_resource.stub(:load_collection?) { true }
|
78
|
-
end
|
79
|
-
|
80
|
-
it "calls the stub" do
|
81
|
-
block.should_receive(:call)
|
82
|
-
controller_resource.load_and_authorize_resource
|
83
|
-
end
|
64
|
+
context "when a singleton stub for the resource exists" do
|
65
|
+
let(:singleton_results) { block }
|
66
|
+
let(:collection_results) { nil }
|
84
67
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
end
|
68
|
+
it "calls the stub" do
|
69
|
+
block.should_receive(:call)
|
70
|
+
controller_resource.load_and_authorize_resource
|
89
71
|
end
|
90
72
|
|
91
|
-
|
92
|
-
|
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
|
93
78
|
|
94
|
-
|
95
|
-
|
96
|
-
|
79
|
+
context "when a collection stub for the resource exists" do
|
80
|
+
let(:singleton_results) { nil }
|
81
|
+
let(:collection_results) { block }
|
97
82
|
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
83
|
+
it "calls the stub" do
|
84
|
+
block.should_receive(:call)
|
85
|
+
controller_resource.load_and_authorize_resource
|
86
|
+
end
|
102
87
|
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
controller_resource.load_and_authorize_resource
|
107
|
-
end
|
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
|
108
91
|
end
|
109
92
|
end
|
110
|
-
|
111
93
|
end
|
112
|
-
|
113
94
|
end
|
114
95
|
end
|
115
96
|
end
|
data/spec/mocks_spec.rb
CHANGED
@@ -15,25 +15,38 @@ module CancanUnitTest
|
|
15
15
|
|
16
16
|
subject(:rspec_test) { TestClass.new(controller) }
|
17
17
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
18
|
+
let(:controller) { double(:controller).as_null_object }
|
19
|
+
let(:options) { double(:options) }
|
20
|
+
let(:block) { -> {} }
|
21
|
+
|
22
|
+
describe "#stub_load_and_authorize_singleton_resource" do
|
22
23
|
|
23
24
|
it "adds the stubbed resource to the controller" do
|
24
25
|
controller.
|
25
26
|
should_receive(:_add_cancan_unit_test_stub).
|
26
|
-
with(:load_and_authorize_resource, :model, options, &block)
|
27
|
+
with(:load_and_authorize_resource, :singleton, :model, options, &block)
|
27
28
|
|
28
29
|
rspec_test.
|
29
|
-
|
30
|
+
stub_load_and_authorize_singleton_resource(:model, options, &block)
|
30
31
|
end
|
31
32
|
|
32
33
|
it "does not require options" do
|
33
|
-
expect
|
34
|
+
expect do
|
35
|
+
rspec_test.
|
36
|
+
stub_load_and_authorize_singleton_resource(:model, &block)
|
37
|
+
end.to_not raise_error
|
34
38
|
end
|
35
39
|
end
|
36
40
|
|
41
|
+
describe "#stub_load_and_authorize_collection_resource" do
|
42
|
+
it "adds the stubed resource collection to the controller" do
|
43
|
+
controller.should_receive(:_add_cancan_unit_test_stub).
|
44
|
+
with(:load_and_authorize_resource, :collection, :model, options, &block)
|
45
|
+
|
46
|
+
rspec_test.
|
47
|
+
stub_load_and_authorize_collection_resource(:model, options, &block)
|
48
|
+
end
|
49
|
+
end
|
37
50
|
end
|
38
51
|
end
|
39
52
|
|
@@ -15,59 +15,84 @@ module CancanUnitTest
|
|
15
15
|
let(:options) { double(:options) }
|
16
16
|
let(:block) { ->{} }
|
17
17
|
|
18
|
-
let(:
|
18
|
+
let(:expected_singleton_definition) do
|
19
19
|
{
|
20
|
+
resource_type: :singleton,
|
20
21
|
model: model,
|
21
22
|
options: options,
|
22
23
|
block: block
|
23
24
|
}
|
24
25
|
end
|
25
26
|
|
27
|
+
let(:expected_collection_definition) do
|
28
|
+
{
|
29
|
+
resource_type: :collection,
|
30
|
+
model: model,
|
31
|
+
options: options,
|
32
|
+
block: block
|
33
|
+
}
|
34
|
+
end
|
26
35
|
describe "retrieving a stored stub definition" do
|
27
36
|
context "no stubs added" do
|
28
37
|
it "returns an empty array" do
|
29
|
-
|
38
|
+
test_controller._get_cancan_unit_test_stubs(:mango).should == []
|
30
39
|
end
|
31
40
|
|
32
41
|
it "does not raise an error" do
|
33
|
-
|
42
|
+
expect { test_controller._get_cancan_unit_test_stubs(:mango) }.not_to raise_error
|
34
43
|
end
|
35
44
|
end
|
36
45
|
|
37
46
|
context "added one stub" do
|
38
47
|
before do
|
39
|
-
test_controller._add_cancan_unit_test_stub(:falaffel, model, options, &block)
|
48
|
+
test_controller._add_cancan_unit_test_stub(:falaffel, :singleton, model, options, &block)
|
40
49
|
end
|
41
50
|
|
42
51
|
it "should be possible to find the stub with a matching key" do
|
43
|
-
test_controller._get_cancan_unit_test_stubs(:falaffel).should == [
|
52
|
+
test_controller._get_cancan_unit_test_stubs(:falaffel).should == [expected_singleton_definition]
|
44
53
|
end
|
45
54
|
|
46
55
|
context "adding another with the same model name" do
|
47
|
-
let(:
|
48
|
-
let(:another_falaffel_stub_definition) do
|
56
|
+
let(:another_singleton_definition) do
|
49
57
|
{
|
50
|
-
|
51
|
-
|
52
|
-
|
58
|
+
resource_type: :singleton,
|
59
|
+
model: model,
|
60
|
+
options: options,
|
61
|
+
block: block
|
53
62
|
}
|
54
63
|
end
|
55
64
|
|
56
|
-
|
57
|
-
|
65
|
+
context "and the same resource type" do
|
66
|
+
before do
|
67
|
+
test_controller._add_cancan_unit_test_stub(:falaffel, :singleton, model, options, &block)
|
68
|
+
end
|
69
|
+
|
70
|
+
it "returns both stubs" do
|
71
|
+
test_controller._get_cancan_unit_test_stubs(:falaffel).should ==
|
72
|
+
[expected_singleton_definition,
|
73
|
+
another_singleton_definition]
|
74
|
+
end
|
58
75
|
end
|
59
76
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
77
|
+
context "and a different resource type" do
|
78
|
+
before do
|
79
|
+
test_controller._add_cancan_unit_test_stub(:falaffel, :collection, model, options, &block)
|
80
|
+
end
|
81
|
+
|
82
|
+
it "returns both stubs" do
|
83
|
+
test_controller._get_cancan_unit_test_stubs(:falaffel).should ==
|
84
|
+
[expected_singleton_definition,
|
85
|
+
expected_collection_definition]
|
86
|
+
end
|
64
87
|
end
|
65
88
|
end
|
66
89
|
|
90
|
+
|
67
91
|
context "adding another with a different model name" do
|
68
92
|
let(:waffle_options) { { syrup: :maple } }
|
69
93
|
let(:waffle_stub_definition) do
|
70
94
|
{
|
95
|
+
resource_type: :singleton,
|
71
96
|
model: model,
|
72
97
|
options: waffle_options,
|
73
98
|
block: block
|
@@ -75,7 +100,7 @@ module CancanUnitTest
|
|
75
100
|
end
|
76
101
|
|
77
102
|
before do
|
78
|
-
test_controller._add_cancan_unit_test_stub(:waffle, model, waffle_options, &block)
|
103
|
+
test_controller._add_cancan_unit_test_stub(:waffle, :singleton, model, waffle_options, &block)
|
79
104
|
end
|
80
105
|
|
81
106
|
it "return the other definition" do
|
data/spec/stub_finder_spec.rb
CHANGED
@@ -10,8 +10,8 @@ module CancanUnitTest
|
|
10
10
|
|
11
11
|
let(:finder) { StubFinder.new(controller, method) }
|
12
12
|
|
13
|
-
describe "#
|
14
|
-
subject { finder.
|
13
|
+
describe "#find_by_singleton" do
|
14
|
+
subject { finder.find_by_singleton(model, options) }
|
15
15
|
|
16
16
|
before do
|
17
17
|
controller.
|
@@ -27,32 +27,68 @@ module CancanUnitTest
|
|
27
27
|
|
28
28
|
context "the controller has a stub for the method" do
|
29
29
|
context "with no matching model" do
|
30
|
-
let(:results) { [{ model: double(:other_model), options: options, block: block }] }
|
30
|
+
let(:results) { [{ resource_type: :singleton, model: double(:other_model), 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) { [{ model: model, options: double(:other_options), block: block }] }
|
35
|
+
let(:results) { [{ resource_type: :singleton, model: model, options: double(:other_options), block: block }] }
|
36
36
|
it { should be_nil }
|
37
37
|
end
|
38
38
|
|
39
39
|
context "with matching model and options" do
|
40
|
-
let(:results) { [{ model: model, options: options, block: block }] }
|
40
|
+
let(:results) { [{ resource_type: :singleton, model: model, options: options, block: block }] }
|
41
41
|
it { should == block }
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
45
45
|
context "the controller has more than one matching stub" do
|
46
|
+
context "with a different resource type" do
|
47
|
+
let(:results) do
|
48
|
+
[
|
49
|
+
{ resource_type: :collection, model: model, options: options, block: -> {} },
|
50
|
+
{ resource_type: :singleton, model: model, options: options, block: block }
|
51
|
+
]
|
52
|
+
end
|
53
|
+
|
54
|
+
it { should == block }
|
55
|
+
end
|
56
|
+
|
57
|
+
context "with the same resource type" do
|
58
|
+
let(:results) do
|
59
|
+
[
|
60
|
+
{ resource_type: :singleton, model: model, options: options, block: block },
|
61
|
+
{ resource_type: :singleton, model: model, options: options, block: block }
|
62
|
+
]
|
63
|
+
end
|
64
|
+
|
65
|
+
it "raises an exception" do
|
66
|
+
expect { finder.find_by_singleton(model, options) }.to raise_error
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
describe "#find_by_collection" do
|
74
|
+
subject { finder.find_by_collection(model, options) }
|
75
|
+
|
76
|
+
before do
|
77
|
+
controller.
|
78
|
+
stub(:_get_cancan_unit_test_stubs).
|
79
|
+
with(method).
|
80
|
+
and_return(results)
|
81
|
+
end
|
82
|
+
|
83
|
+
context "with a different resource type" do
|
46
84
|
let(:results) do
|
47
85
|
[
|
48
|
-
{ model: model, options: options, block:
|
49
|
-
{ model: model, options: options, block: block }
|
86
|
+
{ resource_type: :singleton, model: model, options: options, block: -> {} },
|
87
|
+
{ resource_type: :collection, model: model, options: options, block: block }
|
50
88
|
]
|
51
89
|
end
|
52
90
|
|
53
|
-
it
|
54
|
-
expect { finder.find(model, options) }.to raise_error
|
55
|
-
end
|
91
|
+
it { should == block }
|
56
92
|
end
|
57
93
|
end
|
58
94
|
|
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.2
|
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-
|
11
|
+
date: 2013-07-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: cancan
|
@@ -153,17 +153,21 @@ files:
|
|
153
153
|
- README.md
|
154
154
|
- Rakefile
|
155
155
|
- cancan_unit_test.gemspec
|
156
|
-
- integration/controllers/
|
156
|
+
- integration/controllers/comments_controller_spec.rb
|
157
|
+
- integration/controllers/posts_controller_spec.rb
|
157
158
|
- integration/fixtures/dummy/README.rdoc
|
158
159
|
- integration/fixtures/dummy/Rakefile
|
159
160
|
- integration/fixtures/dummy/app/assets/javascripts/application.js
|
160
161
|
- integration/fixtures/dummy/app/assets/stylesheets/application.css
|
161
162
|
- integration/fixtures/dummy/app/controllers/application_controller.rb
|
163
|
+
- integration/fixtures/dummy/app/controllers/comments_controller.rb
|
162
164
|
- integration/fixtures/dummy/app/controllers/posts_controller.rb
|
163
165
|
- integration/fixtures/dummy/app/helpers/application_helper.rb
|
164
166
|
- integration/fixtures/dummy/app/mailers/.gitkeep
|
165
167
|
- integration/fixtures/dummy/app/models/.gitkeep
|
166
168
|
- integration/fixtures/dummy/app/models/ability.rb
|
169
|
+
- integration/fixtures/dummy/app/models/author.rb
|
170
|
+
- integration/fixtures/dummy/app/models/comment.rb
|
167
171
|
- integration/fixtures/dummy/app/models/post.rb
|
168
172
|
- integration/fixtures/dummy/app/views/layouts/application.html.erb
|
169
173
|
- integration/fixtures/dummy/app/views/posts/index.erb
|
@@ -183,11 +187,10 @@ files:
|
|
183
187
|
- integration/fixtures/dummy/config/initializers/wrap_parameters.rb
|
184
188
|
- integration/fixtures/dummy/config/locales/en.yml
|
185
189
|
- integration/fixtures/dummy/config/routes.rb
|
186
|
-
- integration/fixtures/dummy/db/development.sqlite3
|
187
190
|
- integration/fixtures/dummy/db/migrate/20130709143526_create_posts.rb
|
191
|
+
- integration/fixtures/dummy/db/migrate/20130712144534_create_authors.rb
|
192
|
+
- integration/fixtures/dummy/db/migrate/20130712144551_create_comments.rb
|
188
193
|
- integration/fixtures/dummy/db/schema.rb
|
189
|
-
- integration/fixtures/dummy/db/structure.sql
|
190
|
-
- integration/fixtures/dummy/db/test.sqlite3
|
191
194
|
- integration/fixtures/dummy/lib/assets/.gitkeep
|
192
195
|
- integration/fixtures/dummy/log/.gitkeep
|
193
196
|
- integration/fixtures/dummy/public/404.html
|
@@ -1,25 +0,0 @@
|
|
1
|
-
require 'integration_helper'
|
2
|
-
|
3
|
-
describe PostsController do
|
4
|
-
|
5
|
-
context "not stubbing" do
|
6
|
-
it "uses can can" do
|
7
|
-
post :create
|
8
|
-
assigns(:post).should be_a Post
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
context "stubbing" do
|
13
|
-
let_double(:article)
|
14
|
-
|
15
|
-
before do
|
16
|
-
stub_load_and_authorize_resource(:post) { article }
|
17
|
-
end
|
18
|
-
|
19
|
-
it "uses the stub" do
|
20
|
-
post :create
|
21
|
-
assigns(:post).should == article
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
end
|
Binary file
|
@@ -1 +0,0 @@
|
|
1
|
-
INSERT INTO schema_migrations (version) VALUES ('20130709143526');
|
Binary file
|