switching to Rspec stubbing/mocking - no more RR

This commit is contained in:
Ryan Bates 2012-04-22 14:59:32 -07:00
parent c94de4ab18
commit b37f2d083e
7 changed files with 83 additions and 81 deletions

View File

@ -10,10 +10,9 @@ Gem::Specification.new do |s|
s.files = Dir["{lib,spec}/**/*", "[A-Z]*", "init.rb"] - ["Gemfile.lock"] s.files = Dir["{lib,spec}/**/*", "[A-Z]*", "init.rb"] - ["Gemfile.lock"]
s.require_path = "lib" s.require_path = "lib"
s.add_development_dependency 'rspec', '~> 2.6.0' s.add_development_dependency 'rspec', '~> 2.9.0'
s.add_development_dependency 'rails', '~> 3.0.9' s.add_development_dependency 'rails', '~> 3.0.9'
s.add_development_dependency 'rr', '~> 0.10.11' # 1.0.0 has respond_to? issues: http://github.com/btakita/rr/issues/issue/43 s.add_development_dependency 'supermodel', '~> 0.1.6'
s.add_development_dependency 'supermodel', '~> 0.1.4'
s.rubyforge_project = s.name s.rubyforge_project = s.name
s.required_rubygems_version = ">= 1.3.4" s.required_rubygems_version = ">= 1.3.4"

View File

@ -291,7 +291,8 @@ describe CanCan::Ability do
end end
it "should not match subjects return nil for methods that must match nested a nested conditions hash" do it "should not match subjects return nil for methods that must match nested a nested conditions hash" do
mock(object_with_foo = Object.new).foo { :bar } object_with_foo = Object.new
object_with_foo.should_receive(:foo) { :bar }
@ability.can :read, :arrays, :first => { :foo => :bar } @ability.can :read, :arrays, :first => { :foo => :bar }
@ability.can?(:read, [object_with_foo]).should be_true @ability.can?(:read, [object_with_foo]).should be_true
@ability.can?(:read, []).should be_false @ability.can?(:read, []).should be_false
@ -457,8 +458,8 @@ describe CanCan::Ability do
it "determines model adapter class by asking AbstractAdapter" do it "determines model adapter class by asking AbstractAdapter" do
model_class = Object.new model_class = Object.new
adapter_class = Object.new adapter_class = Object.new
stub(CanCan::ModelAdapters::AbstractAdapter).adapter_class(model_class) { adapter_class } CanCan::ModelAdapters::AbstractAdapter.stub(:adapter_class).with(model_class) { adapter_class }
stub(adapter_class).new(model_class, []) { :adapter_instance } adapter_class.stub(:new).with(model_class, []) { :adapter_instance }
@ability.model_adapter(model_class, :read).should == :adapter_instance @ability.model_adapter(model_class, :read).should == :adapter_instance
end end

View File

@ -5,9 +5,9 @@ describe CanCan::ControllerAdditions do
@params = HashWithIndifferentAccess.new @params = HashWithIndifferentAccess.new
@controller_class = Class.new @controller_class = Class.new
@controller = @controller_class.new @controller = @controller_class.new
stub(@controller).params { @params } @controller.stub(:params) { @params }
stub(@controller).current_user { :current_user } @controller.stub(:current_user) { :current_user }
mock(@controller_class).helper_method(:can?, :cannot?, :current_ability) @controller_class.should_receive(:helper_method).with(:can?, :cannot?, :current_ability)
@controller_class.send(:include, CanCan::ControllerAdditions) @controller_class.send(:include, CanCan::ControllerAdditions)
end end
@ -22,7 +22,7 @@ describe CanCan::ControllerAdditions do
end end
it "authorize! should pass args to current ability" do it "authorize! should pass args to current ability" do
mock(@controller.current_ability).authorize!(:foo, :bar) @controller.current_ability.should_receive(:authorize!).with(:foo, :bar)
@controller.authorize!(:foo, :bar) @controller.authorize!(:foo, :bar)
end end
@ -33,19 +33,23 @@ describe CanCan::ControllerAdditions do
end end
it "load_and_authorize_resource should setup a before filter which passes call to ControllerResource" do it "load_and_authorize_resource should setup a before filter which passes call to ControllerResource" do
stub(CanCan::ControllerResource).new(@controller, nil, :load => true, :authorize => true, :foo => :bar).mock!.process controller_resource = double("controller_resource")
mock(@controller_class).before_filter({}) { |options, block| block.call(@controller) } controller_resource.should_receive(:process)
CanCan::ControllerResource.stub(:new).with(@controller, nil, :load => true, :authorize => true, :foo => :bar) { controller_resource }
@controller_class.should_receive(:before_filter).with({}).and_yield(@controller)
@controller_class.load_and_authorize_resource :foo => :bar @controller_class.load_and_authorize_resource :foo => :bar
end end
it "load_and_authorize_resource should properly pass first argument as the resource name" do it "load_and_authorize_resource should properly pass first argument as the resource name" do
stub(CanCan::ControllerResource).new(@controller, :project, :load => true, :authorize => true, :foo => :bar).mock!.process controller_resource = double("controller_resource")
mock(@controller_class).before_filter({}) { |options, block| block.call(@controller) } controller_resource.should_receive(:process)
CanCan::ControllerResource.stub(:new).with(@controller, :project, :load => true, :authorize => true, :foo => :bar) { controller_resource }
@controller_class.should_receive(:before_filter).with({}).and_yield(@controller)
@controller_class.load_and_authorize_resource :project, :foo => :bar @controller_class.load_and_authorize_resource :project, :foo => :bar
end end
it "load_and_authorize_resource with :prepend should prepend the before filter" do it "load_and_authorize_resource with :prepend should prepend the before filter" do
mock(@controller_class).prepend_before_filter({}) @controller_class.should_receive(:prepend_before_filter).with({})
@controller_class.load_and_authorize_resource :foo => :bar, :prepend => true @controller_class.load_and_authorize_resource :foo => :bar, :prepend => true
end end
@ -54,23 +58,22 @@ describe CanCan::ControllerAdditions do
end end
it "cancan_resource_class should be InheritedResource when class includes InheritedResources::Actions" do it "cancan_resource_class should be InheritedResource when class includes InheritedResources::Actions" do
stub(@controller.class).ancestors { ["InheritedResources::Actions"] } @controller.class.stub(:ancestors) { ["InheritedResources::Actions"] }
@controller.class.cancan_resource_class.should == CanCan::InheritedResource @controller.class.cancan_resource_class.should == CanCan::InheritedResource
end end
it "enable_authorization should call authorize! with controller and action name" do it "enable_authorization should call authorize! with controller and action name" do
@params.merge!(:controller => "projects", :action => "create") @params.merge!(:controller => "projects", :action => "create")
mock(@controller).authorize!("create", "projects") @controller.should_receive(:authorize!).with("create", "projects")
stub(@controller_class).before_filter(:only => :foo, :except => :bar) { |options, block| block.call(@controller) } @controller_class.stub(:before_filter).with(:only => :foo, :except => :bar).and_yield(@controller)
stub(@controller_class).after_filter(:only => :foo, :except => :bar) @controller_class.stub(:after_filter).with(:only => :foo, :except => :bar)
@controller_class.enable_authorization(:only => :foo, :except => :bar) @controller_class.enable_authorization(:only => :foo, :except => :bar)
end end
it "enable_authorization should raise InsufficientAuthorizationCheck when not fully authoried" do it "enable_authorization should raise InsufficientAuthorizationCheck when not fully authoried" do
@params.merge!(:controller => "projects", :action => "create") @params.merge!(:controller => "projects", :action => "create")
stub(@ability).fully_authorized? { false } @controller_class.stub(:before_filter).with(:only => :foo, :except => :bar)
stub(@controller_class).before_filter(:only => :foo, :except => :bar) @controller_class.stub(:after_filter).with(:only => :foo, :except => :bar).and_yield(@controller)
stub(@controller_class).after_filter(:only => :foo, :except => :bar) { |options, block| block.call(@controller) }
lambda { lambda {
@controller_class.enable_authorization(:only => :foo, :except => :bar) @controller_class.enable_authorization(:only => :foo, :except => :bar)
}.should raise_error(CanCan::InsufficientAuthorizationCheck) }.should raise_error(CanCan::InsufficientAuthorizationCheck)
@ -78,29 +81,29 @@ describe CanCan::ControllerAdditions do
it "enable_authorization should not call authorize! when :if is false" do it "enable_authorization should not call authorize! when :if is false" do
@authorize_called = false @authorize_called = false
stub(@controller).authorize? { false } @controller.stub(:authorize?) { false }
stub(@controller).authorize! { @authorize_called = true } @controller.stub(:authorize!) { @authorize_called = true }
mock(@controller_class).before_filter({}) { |options, block| block.call(@controller) } @controller_class.should_receive(:before_filter).with({}).and_yield(@controller)
mock(@controller_class).after_filter({}) { |options, block| block.call(@controller) } @controller_class.should_receive(:after_filter).with({}).and_yield(@controller)
@controller_class.enable_authorization(:if => :authorize?) @controller_class.enable_authorization(:if => :authorize?)
@authorize_called.should be_false @authorize_called.should be_false
end end
it "enable_authorization should not call authorize! when :unless is true" do it "enable_authorization should not call authorize! when :unless is true" do
@authorize_called = false @authorize_called = false
stub(@controller).engine_controller? { true } @controller.stub(:engine_controller?) { true }
stub(@controller).authorize! { @authorize_called = true } @controller.stub(:authorize!) { @authorize_called = true }
mock(@controller_class).before_filter({}) { |options, block| block.call(@controller) } @controller_class.should_receive(:before_filter).with({}).and_yield(@controller)
mock(@controller_class).after_filter({}) { |options, block| block.call(@controller) } @controller_class.should_receive(:after_filter).with({}).and_yield(@controller)
@controller_class.enable_authorization(:unless => :engine_controller?) @controller_class.enable_authorization(:unless => :engine_controller?)
@authorize_called.should be_false @authorize_called.should be_false
end end
it "enable_authorization should pass block to rescue_from CanCan::Unauthorized call" do it "enable_authorization should pass block to rescue_from CanCan::Unauthorized call" do
@block_called = false @block_called = false
mock(@controller_class).before_filter({}) @controller_class.should_receive(:before_filter).with({})
mock(@controller_class).after_filter({}) @controller_class.should_receive(:after_filter).with({})
mock(@controller_class).rescue_from(CanCan::Unauthorized) { |options, block| block.call(:exception) } @controller_class.should_receive(:rescue_from).with(CanCan::Unauthorized).and_yield(:exception)
@controller_class.enable_authorization { |e| @block_called = (e == :exception) } @controller_class.enable_authorization { |e| @block_called = (e == :exception) }
@block_called.should be_true @block_called.should be_true
end end

View File

@ -6,10 +6,10 @@ describe CanCan::ControllerResource do
@controller_class = Class.new @controller_class = Class.new
@controller = @controller_class.new @controller = @controller_class.new
@ability = Ability.new(nil) @ability = Ability.new(nil)
stub(@controller).params { @params } @controller.stub(:params) { @params }
stub(@controller).current_ability { @ability } @controller.stub(:current_ability) { @ability }
stub(@controller).authorize! { |*args| @ability.authorize!(*args) } @controller.stub(:authorize!) { |*args| @ability.authorize!(*args) }
# stub(@controller_class).cancan_skipper { {:authorize => {}, :load => {}} } # @controller_class.stub(:cancan_skipper) { {:authorize => {}, :load => {}} }
end end
it "should load the resource into an instance variable if params[:id] is specified" do it "should load the resource into an instance variable if params[:id] is specified" do
@ -61,7 +61,7 @@ describe CanCan::ControllerResource do
end end
it "should build a collection when on index action when class responds to accessible_by and mark ability as fully authorized" do it "should build a collection when on index action when class responds to accessible_by and mark ability as fully authorized" do
stub(Project).accessible_by(@ability, :index) { :found_projects } Project.stub(:accessible_by).with(@ability, :index) { :found_projects }
@params[:action] = "index" @params[:action] = "index"
CanCan::ControllerResource.new(@controller, :project, :load => true).process CanCan::ControllerResource.new(@controller, :project, :load => true).process
@controller.instance_variable_get(:@project).should be_nil @controller.instance_variable_get(:@project).should be_nil
@ -78,7 +78,7 @@ describe CanCan::ControllerResource do
end end
it "should not use accessible_by when defining abilities through a block" do it "should not use accessible_by when defining abilities through a block" do
stub(Project).accessible_by(@ability) { :found_projects } Project.stub(:accessible_by).with(@ability) { :found_projects }
@params[:action] = "index" @params[:action] = "index"
@ability.can(:read, :projects) { |p| false } @ability.can(:read, :projects) { |p| false }
CanCan::ControllerResource.new(@controller, :load => true).process CanCan::ControllerResource.new(@controller, :load => true).process
@ -89,7 +89,7 @@ describe CanCan::ControllerResource do
it "should not authorize resource in collection action" do it "should not authorize resource in collection action" do
@params[:action] = "index" @params[:action] = "index"
@controller.instance_variable_set(:@project, :some_project) @controller.instance_variable_set(:@project, :some_project)
stub(@controller).authorize!(:index, :projects) { raise CanCan::Unauthorized } @controller.stub(:authorize!).with(:index, :projects) { raise CanCan::Unauthorized }
resource = CanCan::ControllerResource.new(@controller, :authorize => true) resource = CanCan::ControllerResource.new(@controller, :authorize => true)
lambda { resource.process }.should_not raise_error(CanCan::Unauthorized) lambda { resource.process }.should_not raise_error(CanCan::Unauthorized)
end end
@ -97,7 +97,7 @@ describe CanCan::ControllerResource do
it "should authorize parent resource in collection action" do it "should authorize parent resource in collection action" do
@params[:action] = "index" @params[:action] = "index"
@controller.instance_variable_set(:@category, :some_category) @controller.instance_variable_set(:@category, :some_category)
stub(@controller).authorize!(:show, :some_category) { raise CanCan::Unauthorized } @controller.stub(:authorize!).with(:show, :some_category) { raise CanCan::Unauthorized }
resource = CanCan::ControllerResource.new(@controller, :category, :parent => true, :authorize => true) resource = CanCan::ControllerResource.new(@controller, :category, :parent => true, :authorize => true)
lambda { resource.process }.should raise_error(CanCan::Unauthorized) lambda { resource.process }.should raise_error(CanCan::Unauthorized)
end end
@ -105,14 +105,14 @@ describe CanCan::ControllerResource do
it "should perform authorization using controller action and loaded model" do it "should perform authorization using controller action and loaded model" do
@params.merge!(:action => "show", :id => 123) @params.merge!(:action => "show", :id => 123)
@controller.instance_variable_set(:@project, :some_project) @controller.instance_variable_set(:@project, :some_project)
stub(@controller).authorize!(:show, :some_project) { raise CanCan::Unauthorized } @controller.stub(:authorize!).with(:show, :some_project) { raise CanCan::Unauthorized }
resource = CanCan::ControllerResource.new(@controller, :authorize => true) resource = CanCan::ControllerResource.new(@controller, :authorize => true)
lambda { resource.process }.should raise_error(CanCan::Unauthorized) lambda { resource.process }.should raise_error(CanCan::Unauthorized)
end end
it "should not perform authorization using controller action when no loaded model" do it "should not perform authorization using controller action when no loaded model" do
@params.merge!(:action => "show", :id => 123) @params.merge!(:action => "show", :id => 123)
stub(@controller).authorize!(:show, :projects) { raise CanCan::Unauthorized } @controller.stub(:authorize!).with(:show, :projects) { raise CanCan::Unauthorized }
resource = CanCan::ControllerResource.new(@controller, :authorize => true) resource = CanCan::ControllerResource.new(@controller, :authorize => true)
lambda { resource.process }.should_not raise_error(CanCan::Unauthorized) lambda { resource.process }.should_not raise_error(CanCan::Unauthorized)
end end
@ -124,7 +124,7 @@ describe CanCan::ControllerResource do
end end
it "should load a collection resource when on custom action with no id param" do it "should load a collection resource when on custom action with no id param" do
stub(Project).accessible_by(@ability, :sort) { :found_projects } Project.stub(:accessible_by).with(@ability, :sort) { :found_projects }
@params[:action] = "sort" @params[:action] = "sort"
CanCan::ControllerResource.new(@controller, :load => true).process CanCan::ControllerResource.new(@controller, :load => true).process
@controller.instance_variable_get(:@project).should be_nil @controller.instance_variable_get(:@project).should be_nil
@ -133,7 +133,7 @@ describe CanCan::ControllerResource do
it "should build a resource when on custom new action even when params[:id] exists" do it "should build a resource when on custom new action even when params[:id] exists" do
@params.merge!(:action => "build", :id => 123) @params.merge!(:action => "build", :id => 123)
stub(Project).new { :some_project } Project.stub(:new) { :some_project }
CanCan::ControllerResource.new(@controller, :load => true, :new => :build).process CanCan::ControllerResource.new(@controller, :load => true, :new => :build).process
@controller.instance_variable_get(:@project).should == :some_project @controller.instance_variable_get(:@project).should == :some_project
end end
@ -173,27 +173,27 @@ describe CanCan::ControllerResource do
it "should load resource through the association of another parent resource using instance variable" do it "should load resource through the association of another parent resource using instance variable" do
@params.merge!(:action => "show", :id => 123) @params.merge!(:action => "show", :id => 123)
category = Object.new category = double("category", :projects => double("projects"))
category.projects.stub(:find).with(123) { :some_project }
@controller.instance_variable_set(:@category, category) @controller.instance_variable_set(:@category, category)
stub(category).projects.stub!.find(123) { :some_project }
CanCan::ControllerResource.new(@controller, :load => true, :through => :category).process CanCan::ControllerResource.new(@controller, :load => true, :through => :category).process
@controller.instance_variable_get(:@project).should == :some_project @controller.instance_variable_get(:@project).should == :some_project
end end
it "should load resource through the custom association name" do it "should load resource through the custom association name" do
@params.merge!(:action => "show", :id => 123) @params.merge!(:action => "show", :id => 123)
category = Object.new category = double("category", :custom_projects => double("custom_projects"))
category.custom_projects.stub(:find).with(123) { :some_project }
@controller.instance_variable_set(:@category, category) @controller.instance_variable_set(:@category, category)
stub(category).custom_projects.stub!.find(123) { :some_project }
CanCan::ControllerResource.new(@controller, :load => true, :through => :category, :through_association => :custom_projects).process CanCan::ControllerResource.new(@controller, :load => true, :through => :category, :through_association => :custom_projects).process
@controller.instance_variable_get(:@project).should == :some_project @controller.instance_variable_get(:@project).should == :some_project
end end
it "should load resource through the association of another parent resource using method" do it "should load resource through the association of another parent resource using method" do
@params.merge!(:action => "show", :id => 123) @params.merge!(:action => "show", :id => 123)
category = Object.new category = double("category", :projects => double("projects"))
stub(@controller).category { category } @controller.stub(:category) { category }
stub(category).projects.stub!.find(123) { :some_project } category.projects.stub(:find).with(123) { :some_project }
CanCan::ControllerResource.new(@controller, :load => true, :through => :category).process CanCan::ControllerResource.new(@controller, :load => true, :through => :category).process
@controller.instance_variable_get(:@project).should == :some_project @controller.instance_variable_get(:@project).should == :some_project
end end
@ -223,16 +223,16 @@ describe CanCan::ControllerResource do
@params.merge!(:action => "index") @params.merge!(:action => "index")
category = Object.new category = Object.new
@controller.instance_variable_set(:@category, category) @controller.instance_variable_set(:@category, category)
stub(@controller).authorize!(:index, category => :projects) { raise CanCan::Unauthorized } @controller.stub(:authorize!).with(:index, category => :projects) { raise CanCan::Unauthorized }
resource = CanCan::ControllerResource.new(@controller, :authorize => true, :through => :category) resource = CanCan::ControllerResource.new(@controller, :authorize => true, :through => :category)
lambda { resource.process }.should raise_error(CanCan::Unauthorized) lambda { resource.process }.should raise_error(CanCan::Unauthorized)
end end
it "should load through first matching if multiple are given" do it "should load through first matching if multiple are given" do
@params.merge!(:action => "show", :id => 123) @params.merge!(:action => "show", :id => 123)
category = Object.new category = double("category", :projects => double("projects"))
category.projects.stub(:find).with(123) { :some_project }
@controller.instance_variable_set(:@category, category) @controller.instance_variable_set(:@category, category)
stub(category).projects.stub!.find(123) { :some_project }
CanCan::ControllerResource.new(@controller, :load => true, :through => [:category, :user]).process CanCan::ControllerResource.new(@controller, :load => true, :through => [:category, :user]).process
@controller.instance_variable_get(:@project).should == :some_project @controller.instance_variable_get(:@project).should == :some_project
end end
@ -241,7 +241,7 @@ describe CanCan::ControllerResource do
@params.merge!(:action => "show", :id => nil) @params.merge!(:action => "show", :id => nil)
category = Object.new category = Object.new
@controller.instance_variable_set(:@category, category) @controller.instance_variable_set(:@category, category)
stub(category).project { :some_project } category.stub(:project) { :some_project }
CanCan::ControllerResource.new(@controller, :load => true, :through => :category, :singleton => true).process CanCan::ControllerResource.new(@controller, :load => true, :through => :category, :singleton => true).process
@controller.instance_variable_get(:@project).should == :some_project @controller.instance_variable_get(:@project).should == :some_project
end end
@ -271,7 +271,7 @@ describe CanCan::ControllerResource do
it "should only authorize :show action on parent resource" do it "should only authorize :show action on parent resource" do
project = Project.create! project = Project.create!
@params.merge!(:action => "new", :project_id => project.id) @params.merge!(:action => "new", :project_id => project.id)
stub(@controller).authorize!(:show, project) { raise CanCan::Unauthorized } @controller.stub(:authorize!).with(:show, project) { raise CanCan::Unauthorized }
resource = CanCan::ControllerResource.new(@controller, :project, :load => true, :authorize => true, :parent => true) resource = CanCan::ControllerResource.new(@controller, :project, :load => true, :authorize => true, :parent => true)
lambda { resource.process }.should raise_error(CanCan::Unauthorized) lambda { resource.process }.should raise_error(CanCan::Unauthorized)
end end
@ -301,7 +301,7 @@ describe CanCan::ControllerResource do
it "should not authorize based on resource name if class is false because we don't do class level authorization anymore" do it "should not authorize based on resource name if class is false because we don't do class level authorization anymore" do
@params.merge!(:action => "show", :id => 123) @params.merge!(:action => "show", :id => 123)
stub(@controller).authorize!(:show, :projects) { raise CanCan::Unauthorized } @controller.stub(:authorize!).with(:show, :projects) { raise CanCan::Unauthorized }
resource = CanCan::ControllerResource.new(@controller, :authorize => true, :class => false) resource = CanCan::ControllerResource.new(@controller, :authorize => true, :class => false)
lambda { resource.process }.should_not raise_error(CanCan::Unauthorized) lambda { resource.process }.should_not raise_error(CanCan::Unauthorized)
end end
@ -309,7 +309,7 @@ describe CanCan::ControllerResource do
it "should load and authorize using custom instance name" do it "should load and authorize using custom instance name" do
project = Project.create! project = Project.create!
@params.merge!(:action => "show", :id => project.id) @params.merge!(:action => "show", :id => project.id)
stub(@controller).authorize!(:show, project) { raise CanCan::Unauthorized } @controller.stub(:authorize!).with(:show, project) { raise CanCan::Unauthorized }
resource = CanCan::ControllerResource.new(@controller, :load => true, :authorize => true, :instance_name => :custom_project) resource = CanCan::ControllerResource.new(@controller, :load => true, :authorize => true, :instance_name => :custom_project)
lambda { resource.process }.should raise_error(CanCan::Unauthorized) lambda { resource.process }.should raise_error(CanCan::Unauthorized)
@controller.instance_variable_get(:@custom_project).should == project @controller.instance_variable_get(:@custom_project).should == project
@ -333,7 +333,7 @@ describe CanCan::ControllerResource do
it "should authorize each new attribute in the create action" do it "should authorize each new attribute in the create action" do
@params.merge!(:action => "create", :project => {:name => "foo"}) @params.merge!(:action => "create", :project => {:name => "foo"})
@controller.instance_variable_set(:@project, :some_project) @controller.instance_variable_set(:@project, :some_project)
mock(@controller).authorize!(:create, :some_project, :name) @ability.should_receive(:authorize!).with(:create, :some_project, :name)
CanCan::ControllerResource.new(@controller, :authorize => true).process CanCan::ControllerResource.new(@controller, :authorize => true).process
end end
@ -347,14 +347,14 @@ describe CanCan::ControllerResource do
it "should authorize each new attribute in the update action" do it "should authorize each new attribute in the update action" do
@params.merge!(:action => "update", :id => 123, :project => {:name => "foo"}) @params.merge!(:action => "update", :id => 123, :project => {:name => "foo"})
@controller.instance_variable_set(:@project, :some_project) @controller.instance_variable_set(:@project, :some_project)
mock(@controller).authorize!(:update, :some_project, :name) @ability.should_receive(:authorize!).with(:update, :some_project, :name)
CanCan::ControllerResource.new(@controller, :authorize => true).process CanCan::ControllerResource.new(@controller, :authorize => true).process
end end
it "should fetch member through method when instance variable is not provided" do it "should fetch member through method when instance variable is not provided" do
stub(@controller).project { :some_project } @controller.stub(:project) { :some_project }
@params.merge!(:action => "show", :id => 123) @params.merge!(:action => "show", :id => 123)
stub(@controller).authorize!(:show, :some_project) { raise CanCan::Unauthorized } @controller.stub(:authorize!).with(:show, :some_project) { raise CanCan::Unauthorized }
resource = CanCan::ControllerResource.new(@controller, :authorize => true) resource = CanCan::ControllerResource.new(@controller, :authorize => true)
lambda { resource.process }.should raise_error(CanCan::Unauthorized) lambda { resource.process }.should raise_error(CanCan::Unauthorized)
end end
@ -398,7 +398,7 @@ describe CanCan::ControllerResource do
# end # end
# it "should skip resource behavior for :only actions in array" do # it "should skip resource behavior for :only actions in array" do
# stub(@controller_class).cancan_skipper { {:load => {nil => {:only => [:index, :show]}}} } # @controller_class.stub(:cancan_skipper) { {:load => {nil => {:only => [:index, :show]}}} }
# @params.merge!(:action => "index") # @params.merge!(:action => "index")
# CanCan::ControllerResource.new(@controller).skip?(:load).should be_true # CanCan::ControllerResource.new(@controller).skip?(:load).should be_true
# CanCan::ControllerResource.new(@controller, :some_resource).skip?(:load).should be_false # CanCan::ControllerResource.new(@controller, :some_resource).skip?(:load).should be_false
@ -409,7 +409,7 @@ describe CanCan::ControllerResource do
# end # end
# #
# it "should skip resource behavior for :only one action on resource" do # it "should skip resource behavior for :only one action on resource" do
# stub(@controller_class).cancan_skipper { {:authorize => {:project => {:only => :index}}} } # @controller_class.stub(:cancan_skipper) { {:authorize => {:project => {:only => :index}}} }
# @params.merge!(:action => "index") # @params.merge!(:action => "index")
# CanCan::ControllerResource.new(@controller).skip?(:authorize).should be_false # CanCan::ControllerResource.new(@controller).skip?(:authorize).should be_false
# CanCan::ControllerResource.new(@controller, :project).skip?(:authorize).should be_true # CanCan::ControllerResource.new(@controller, :project).skip?(:authorize).should be_true
@ -418,7 +418,7 @@ describe CanCan::ControllerResource do
# end # end
# #
# it "should skip resource behavior :except actions in array" do # it "should skip resource behavior :except actions in array" do
# stub(@controller_class).cancan_skipper { {:load => {nil => {:except => [:index, :show]}}} } # @controller_class.stub(:cancan_skipper) { {:load => {nil => {:except => [:index, :show]}}} }
# @params.merge!(:action => "index") # @params.merge!(:action => "index")
# CanCan::ControllerResource.new(@controller).skip?(:load).should be_false # CanCan::ControllerResource.new(@controller).skip?(:load).should be_false
# @params.merge!(:action => "show") # @params.merge!(:action => "show")
@ -429,7 +429,7 @@ describe CanCan::ControllerResource do
# end # end
# #
# it "should skip resource behavior :except one action on resource" do # it "should skip resource behavior :except one action on resource" do
# stub(@controller_class).cancan_skipper { {:authorize => {:project => {:except => :index}}} } # @controller_class.stub(:cancan_skipper) { {:authorize => {:project => {:except => :index}}} }
# @params.merge!(:action => "index") # @params.merge!(:action => "index")
# CanCan::ControllerResource.new(@controller, :project).skip?(:authorize).should be_false # CanCan::ControllerResource.new(@controller, :project).skip?(:authorize).should be_false
# @params.merge!(:action => "other_action") # @params.merge!(:action => "other_action")
@ -438,7 +438,7 @@ describe CanCan::ControllerResource do
# end # end
# #
# it "should skip loading and authorization" do # it "should skip loading and authorization" do
# stub(@controller_class).cancan_skipper { {:authorize => {nil => {}}, :load => {nil => {}}} } # @controller_class.stub(:cancan_skipper) { {:authorize => {nil => {}}, :load => {nil => {}}} }
# @params.merge!(:action => "new") # @params.merge!(:action => "new")
# resource = CanCan::ControllerResource.new(@controller) # resource = CanCan::ControllerResource.new(@controller)
# lambda { resource.load_and_authorize_resource }.should_not raise_error # lambda { resource.load_and_authorize_resource }.should_not raise_error

View File

@ -6,36 +6,36 @@ describe CanCan::InheritedResource do
@controller_class = Class.new @controller_class = Class.new
@controller = @controller_class.new @controller = @controller_class.new
@ability = Ability.new(nil) @ability = Ability.new(nil)
stub(@controller).params { @params } @controller.stub(:params) { @params }
stub(@controller).current_ability { @ability } @controller.stub(:current_ability) { @ability }
# stub(@controller_class).cancan_skipper { {:authorize => {}, :load => {}} } # @controller_class.stub(:cancan_skipper) { {:authorize => {}, :load => {}} }
end end
it "show should load resource through @controller.resource" do it "show should load resource through @controller.resource" do
@params.merge!(:action => "show", :id => 123) @params.merge!(:action => "show", :id => 123)
stub(@controller).resource { :project_resource } @controller.stub(:resource) { :project_resource }
CanCan::InheritedResource.new(@controller, :load => true).process CanCan::InheritedResource.new(@controller, :load => true).process
@controller.instance_variable_get(:@project).should == :project_resource @controller.instance_variable_get(:@project).should == :project_resource
end end
it "new should load through @controller.build_resource" do it "new should load through @controller.build_resource" do
@params[:action] = "new" @params[:action] = "new"
stub(@controller).build_resource { :project_resource } @controller.stub(:build_resource) { :project_resource }
CanCan::InheritedResource.new(@controller, :load => true).process CanCan::InheritedResource.new(@controller, :load => true).process
@controller.instance_variable_get(:@project).should == :project_resource @controller.instance_variable_get(:@project).should == :project_resource
end end
it "index should load through @controller.association_chain when parent" do it "index should load through @controller.association_chain when parent" do
@params[:action] = "index" @params[:action] = "index"
stub(@controller).association_chain { @controller.instance_variable_set(:@project, :project_resource) } @controller.stub(:association_chain) { @controller.instance_variable_set(:@project, :project_resource) }
CanCan::InheritedResource.new(@controller, :load => true, :parent => true).process CanCan::InheritedResource.new(@controller, :load => true, :parent => true).process
@controller.instance_variable_get(:@project).should == :project_resource @controller.instance_variable_get(:@project).should == :project_resource
end end
it "index should load through @controller.end_of_association_chain" do it "index should load through @controller.end_of_association_chain" do
@params[:action] = "index" @params[:action] = "index"
stub(Project).accessible_by(@ability, :index) { :projects } Project.stub(:accessible_by).with(@ability, :index) { :projects }
stub(@controller).end_of_association_chain { Project } @controller.stub(:end_of_association_chain) { Project }
CanCan::InheritedResource.new(@controller, :load => true).process CanCan::InheritedResource.new(@controller, :load => true).process
@controller.instance_variable_get(:@projects).should == :projects @controller.instance_variable_get(:@projects).should == :projects
end end

View File

@ -3,13 +3,13 @@ require "spec_helper"
describe "be_able_to" do describe "be_able_to" do
it "delegates to can?" do it "delegates to can?" do
object = Object.new object = Object.new
mock(object).can?(:read, 123) { true } object.should_receive(:can?).with(:read, 123) { true }
object.should be_able_to(:read, 123) object.should be_able_to(:read, 123)
end end
it "reports a nice failure message for should" do it "reports a nice failure message for should" do
object = Object.new object = Object.new
mock(object).can?(:read, 123) { false } object.should_receive(:can?).with(:read, 123) { false }
expect do expect do
object.should be_able_to(:read, 123) object.should be_able_to(:read, 123)
end.should raise_error('expected to be able to :read 123') end.should raise_error('expected to be able to :read 123')
@ -17,7 +17,7 @@ describe "be_able_to" do
it "reports a nice failure message for should not" do it "reports a nice failure message for should not" do
object = Object.new object = Object.new
mock(object).can?(:read, 123) { true } object.should_receive(:can?).with(:read, 123) { true }
expect do expect do
object.should_not be_able_to(:read, 123) object.should_not be_able_to(:read, 123)
end.should raise_error('expected not to be able to :read 123') end.should raise_error('expected not to be able to :read 123')
@ -25,7 +25,7 @@ describe "be_able_to" do
it "delegates additional arguments to can? and reports in failure message" do it "delegates additional arguments to can? and reports in failure message" do
object = Object.new object = Object.new
mock(object).can?(:read, 123, 456) { false } object.should_receive(:can?).with(:read, 123, 456) { false }
expect do expect do
object.should be_able_to(:read, 123, 456) object.should be_able_to(:read, 123, 456)
end.should raise_error('expected to be able to :read 123 456') end.should raise_error('expected to be able to :read 123 456')

View File

@ -13,7 +13,6 @@ RSpec.configure do |config|
config.filter_run :focus => true config.filter_run :focus => true
config.run_all_when_everything_filtered = true config.run_all_when_everything_filtered = true
config.mock_with :rr
config.before(:each) do config.before(:each) do
Project.delete_all Project.delete_all
Category.delete_all Category.delete_all