-
Notifications
You must be signed in to change notification settings - Fork 213
How we could use cancan
David Corking edited this page Jul 22, 2013
·
1 revision
This transcript shows how cancan abilities could be tested. This rspec debugger session is in the context of a toy example of an ability of can :read :all
$ bundle exec rspec spec/controllers/organizations_controller_spec.rb
............../home/david1/LocalSupport-dcorking/spec/controllers/organizations_controller_spec.rb:186
Organization.stub(:new).with({'these' => 'params'}) { mock_organization(:save => true) }
[181, 190] in /home/david1/LocalSupport-dcorking/spec/controllers/organizations_controller_spec.rb
181 end
182
183 describe "with valid params" do
184 it "assigns a newly created organization as @organization" do
185 debugger
=> 186 Organization.stub(:new).with({'these' => 'params'}) { mock_organization(:save => true) }
187 post :create, :organization => {'these' => 'params'}
188 assigns(:organization).should be(mock_organization)
189 end
190
(rdb:1) cookies
{}
(rdb:1) user
*** NameError Exception: undefined local variable or method `user' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_7::Nested_1::Nested_1:0xaefdf58>
(rdb:1) controller
#<OrganizationsController:0xaf02634 @_routes=nil, @_action_has_layout=true, @_view_context_class=nil, @_headers={"Content-Type"=>"text/html"}, @_status=200, @_request=#<ActionController::TestRequest:0xaf03480 @env={"rack.version"=>[1, 1], "rack.input"=>#<StringIO:0xadb18e8>, "rack.errors"=>#<StringIO:0xade8ff0>, "rack.multithread"=>true, "rack.multiprocess"=>true, "rack.run_once"=>false, "REQUEST_METHOD"=>"GET", "SERVER_NAME"=>"example.org", "SERVER_PORT"=>"80", "QUERY_STRING"=>"", "rack.url_scheme"=>"http", "HTTPS"=>"off", "SCRIPT_NAME"=>"", "CONTENT_LENGTH"=>"0", "action_dispatch.routes"=>#<ActionDispatch::Routing::RouteSet:0xb1bdaa4>, "action_dispatch.parameter_filter"=>[:password], "action_dispatch.secret_token"=>"98f7bafde9e9f5cab69c8e3f25a0f70bf8f97f69594f87fa3caa42e58aa0b1508f1c2efac3123a6975386fc8b9c742df9a844b0024eea553a7eb6808a4a9a02a", "action_dispatch.show_exceptions"=>false, "HTTP_HOST"=>"test.host", "REMOTE_ADDR"=>"0.0.0.0", "HTTP_USER_AGENT"=>"Rails Testing", "rack.session"=>{}, "rack.session.options"=>{:key=>"rack.session", :path=>"/", :domain=>nil, :expire_after=>nil, :secure=>false, :httponly=>true, :defer=>false, :renew=>false, :sidbits=>128, :cookie_only=>true, :secure_random=>SecureRandom, :id=>"98a65341c266ab9e19822673dadb80ef"}, "action_controller.instance"=>#<OrganizationsController:0xaf02634 ...>, "warden"=>Warden::Proxy:97226860 @config={:default_scope=>:user, :scope_defaults=>{}, :default_strategies=>{:user=>[:rememberable, :database_authenticatable]}, :intercept_401=>false, :failure_app=>#<Devise::Delegator:0xb3e1dd0>}, "rack.request.cookie_hash"=>{}}, @cookies={}>, @_response=nil, @_params={}, @mock_proxy=#<RSpec::Mocks::Proxy:0xb7df630 @object=#<OrganizationsController:0xaf02634 ...>, @name=nil, @error_generator=#<RSpec::Mocks::ErrorGenerator:0xb7df5f4 @declared_as="Mock", @target=#<OrganizationsController:0xaf02634 ...>, @name=nil, @opts={}>, @expectation_ordering=#<RSpec::Mocks::OrderGroup:0xb640ba8 @ordering=[]>, @messages_received=[], @options={}, @already_proxied_respond_to=false, @null_object=false, @method_double={:current_user=>{:expectations=>[], :stubs=>[#<RSpec::Mocks::MessageExpectation:0xb7dd858 @error_generator=#<RSpec::Mocks::ErrorGenerator:0xb7df5f4 @declared_as="Mock", @target=#<OrganizationsController:0xaf02634 ...>, @name=nil, @opts={}>, @expected_from="/home/david1/LocalSupport-dcorking/spec/controllers/organizations_controller_spec.rb:179:in `block (4 levels) in <top (required)>'", @method_double={...}, @message=:current_user, @actual_received_count=0, @expected_received_count=:any, @argument_list_matcher=#<RSpec::Mocks::ArgumentListMatcher:0xb7dd81c @expected_args=[#<RSpec::Mocks::ArgumentMatchers::AnyArgsMatcher:0xb7dd830>], @block=nil, @match_any_args=true, @matchers=nil>, @order_group=#<RSpec::Mocks::OrderGroup:0xb640ba8 @ordering=[]>, @exactly=nil, @at_most=nil, @at_least=nil, @args_to_yield=[], @failed_fast=nil, @eval_context=nil, @implementation=#<RSpec::Mocks::Implementation:0xb7dd7b8 @terminal_action=#<RSpec::Mocks::AndReturnImplementation:0xb7dd77c @values_to_return=[#<User:0x5b6c8da @name="User_1016">]>>>]}}>>
(rdb:1) can?
*** NoMethodError Exception: undefined method `can?' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_7::Nested_1::Nested_1:0xaefdf58>
(rdb:1) controller.can?
*** ArgumentError Exception: wrong number of arguments (0 for 2)
(rdb:1) controller.can? (:create, Organization)
*** SyntaxError Exception: /home/david1/LocalSupport-dcorking/spec/controllers/organizations_controller_spec.rb:186: syntax error, unexpected ',', expecting ')'
controller.can? (:create, Organization)
^
(rdb:1) controller.can?(:edit, Organization.new())
false
(rdb:1) controller.can?(:create, Organization.new())
false
(rdb:1) controller.can?(:show, Organization.new())
true
(rdb:1) controller.can?(:read, Organization.new())
true
(rdb:1) controller.can?(:foo, Organization.new())
false
(rdb:1) controller.can?(:delete, Organization.new())
false
(rdb:1) controller.can?(:create, Organization)
false
(rdb:1) controller.can?(:index, Organization)
true
(rdb:1) controller.can?(:index, Organization)
true
(rdb:1) controller.can?(:update, Organization.new())
false
(rdb:1) load '/home/david1/LocalSupport-dcorking/app/models/ability.rb'
true
(rdb:1) controller.can?(:update, Organization.new())
false
(rdb:1) controller.can?(:edit, Organization.new())
false