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.
Files changed (29) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/Gemfile.lock +1 -1
  4. data/README.md +2 -1
  5. data/integration/controllers/comments_controller_spec.rb +36 -0
  6. data/integration/controllers/posts_controller_spec.rb +36 -0
  7. data/integration/fixtures/dummy/app/controllers/comments_controller.rb +11 -0
  8. data/integration/fixtures/dummy/app/controllers/posts_controller.rb +1 -1
  9. data/integration/fixtures/dummy/app/models/author.rb +3 -0
  10. data/integration/fixtures/dummy/app/models/comment.rb +3 -0
  11. data/integration/fixtures/dummy/config/routes.rb +3 -57
  12. data/integration/fixtures/dummy/db/migrate/20130709143526_create_posts.rb +1 -0
  13. data/integration/fixtures/dummy/db/migrate/20130712144534_create_authors.rb +9 -0
  14. data/integration/fixtures/dummy/db/migrate/20130712144551_create_comments.rb +10 -0
  15. data/integration/fixtures/dummy/db/schema.rb +15 -1
  16. data/lib/cancan_unit_test/action_controller/stub_registry.rb +4 -4
  17. data/lib/cancan_unit_test/cancan/controller_resource.rb +10 -8
  18. data/lib/cancan_unit_test/mocks.rb +5 -2
  19. data/lib/cancan_unit_test/stub_finder.rb +19 -10
  20. data/lib/cancan_unit_test/version.rb +1 -1
  21. data/spec/cancan/controller_resource_spec.rb +35 -54
  22. data/spec/mocks_spec.rb +20 -7
  23. data/spec/rails/action_controller/stub_registry_spec.rb +42 -17
  24. data/spec/stub_finder_spec.rb +46 -10
  25. metadata +9 -6
  26. data/integration/controllers/mocks_spec.rb +0 -25
  27. data/integration/fixtures/dummy/db/development.sqlite3 +0 -0
  28. data/integration/fixtures/dummy/db/structure.sql +0 -1
  29. data/integration/fixtures/dummy/db/test.sqlite3 +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d6177faaa08bc88c1f286f8c76f68155b41f4f78
4
- data.tar.gz: 6db7a439ae237ff7a0b349fa8804f218fd4a8d74
3
+ metadata.gz: aa942f63f5ef9c7e8d0e568d49fe7a6bc2781c14
4
+ data.tar.gz: e2814f79002bf4a8594ad1632fa2124bb1b970a6
5
5
  SHA512:
6
- metadata.gz: 3b32f869d10ce6f4aa136d676e244a46800d8d6b7cf83043cbb86198a2b8cc7f754a300b5515fad5c45a09fbbed7184234def2e942865c879fe8fd7e45c1956a
7
- data.tar.gz: 11414c0895523b948e26d3f081302e6ab433955ccdb2823defecb851f586b1d1a507b7bc3a7ea3492087bd66e09c25d232a8c86c38f8a88938e1f7027550762a
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
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cancan-unit-test (0.0.1)
4
+ cancan-unit-test (0.0.2)
5
5
  activesupport
6
6
  cancan
7
7
 
data/README.md CHANGED
@@ -20,7 +20,8 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- stub_load_and_authorize_resource(:model_name, options_hash)
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
@@ -0,0 +1,11 @@
1
+ class CommentsController < ApplicationController
2
+ load_and_authorize_resource :post
3
+ load_and_authorize_resource
4
+ load_and_authorize_resource :author, instance_name: :post_author, through: :post, parent: false
5
+
6
+ def index
7
+ head :ok
8
+ end
9
+
10
+ end
11
+
@@ -1,5 +1,5 @@
1
1
  class PostsController < ApplicationController
2
- load_and_authorize_resource
2
+ load_and_authorize_resource :post
3
3
 
4
4
  def create
5
5
  render :index
@@ -0,0 +1,3 @@
1
+ class Author < ActiveRecord::Base
2
+ attr_accessible :name
3
+ end
@@ -0,0 +1,3 @@
1
+ class Comment < ActiveRecord::Base
2
+ attr_accessible :content
3
+ 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
@@ -1,6 +1,7 @@
1
1
  class CreatePosts < ActiveRecord::Migration
2
2
  def change
3
3
  create_table :posts do |t|
4
+ t.references :author
4
5
  t.string :title
5
6
  t.text :content
6
7
 
@@ -0,0 +1,9 @@
1
+ class CreateAuthors < ActiveRecord::Migration
2
+ def change
3
+ create_table :authors do |t|
4
+ t.string :name
5
+
6
+ t.timestamps
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ class CreateComments < ActiveRecord::Migration
2
+ def change
3
+ create_table :comments do |t|
4
+ t.references :post
5
+ t.string :content
6
+
7
+ t.timestamps
8
+ end
9
+ end
10
+ 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 => 20130709143526) do
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 = cancan_unit_test_stubs[method] ||= []
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
- if stub.nil?
14
- _original_load_and_authorize_resource
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
- if load_instance?
17
- self.resource_instance = stub.call
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 stub_load_and_authorize_resource(model, options={}, &block)
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 find(model, options)
9
- stub_list = filter_stub_list(model, options)
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
- stub_list.first[:block]
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 filter_stub_list(model, options)
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[:model] == model &&
23
- stub[:options] == options
30
+ stub[:resource_type] == resource_type &&
31
+ stub[:model] == model &&
32
+ stub[:options] == options
24
33
  end
25
34
  end
26
35
 
@@ -1,3 +1,3 @@
1
1
  module CancanUnitTest
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -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(:find).with(:the_model_name, options) { stub_finder_results }
43
- controller_resource.stub(:load_instance?) { load_instance }
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(:stub_finder_results) { nil }
48
- let(:load_instance) { true }
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
- it "assigns the stub to resource_instance" do
68
- controller_resource.should_receive(:resource_instance=).with(block_result)
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
- context "when loading a collection of resource instances" do
74
- let(:load_instance) { false }
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
- it "assigns the stub to collection_instance" do
86
- controller_resource.should_receive(:collection_instance=).with(block_result)
87
- controller_resource.load_and_authorize_resource
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
- context "when loading neither an instance, nor a collection, however that may happen?" do
92
- let(:load_instance) { false }
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
- before do
95
- controller_resource.stub(:load_collection?) { false }
96
- end
79
+ context "when a collection stub for the resource exists" do
80
+ let(:singleton_results) { nil }
81
+ let(:collection_results) { block }
97
82
 
98
- it "does not call the stub" do
99
- block.should_not_receive(:call)
100
- controller_resource.load_and_authorize_resource
101
- end
83
+ it "calls the stub" do
84
+ block.should_receive(:call)
85
+ controller_resource.load_and_authorize_resource
86
+ end
102
87
 
103
- it "does not assign the stub to anything" do
104
- controller_resource.should_not_receive(:resource_instance=).with(block_result)
105
- controller_resource.should_not_receive(:collection_instance=).with(block_result)
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
- describe "#stub_load_and_authorize_resource" do
19
- let(:controller) { double(:controller).as_null_object }
20
- let(:options) { double(:options) }
21
- let(:block) { -> {} }
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
- stub_load_and_authorize_resource(:model, options, &block)
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 { rspec_test. stub_load_and_authorize_resource(:model, &block) }.to_not raise_error
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(:expected_stub_definition) do
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
- test_controller._get_cancan_unit_test_stubs(:mango).should == []
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
- expect { test_controller._get_cancan_unit_test_stubs(:mango) }.not_to raise_error
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 == [expected_stub_definition]
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(:another_falaffel_model) { double(:model) }
48
- let(:another_falaffel_stub_definition) do
56
+ let(:another_singleton_definition) do
49
57
  {
50
- model: another_falaffel_model,
51
- options: options,
52
- block: block
58
+ resource_type: :singleton,
59
+ model: model,
60
+ options: options,
61
+ block: block
53
62
  }
54
63
  end
55
64
 
56
- before do
57
- test_controller._add_cancan_unit_test_stub(:falaffel, another_falaffel_model, options, &block)
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
- it "returns both stubs" do
61
- test_controller._get_cancan_unit_test_stubs(:falaffel).should ==
62
- [expected_stub_definition,
63
- another_falaffel_stub_definition]
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
@@ -10,8 +10,8 @@ module CancanUnitTest
10
10
 
11
11
  let(:finder) { StubFinder.new(controller, method) }
12
12
 
13
- describe "#find" do
14
- subject { finder.find(model, options) }
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: 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 "raises an exception" do
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.1
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 00:00:00.000000000 Z
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/mocks_spec.rb
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
@@ -1 +0,0 @@
1
- INSERT INTO schema_migrations (version) VALUES ('20130709143526');