using supermodel in specs to remove some of the model stubs

This commit is contained in:
Ryan Bates 2010-10-08 11:46:41 -07:00
parent 67cd83dd96
commit f901c367fc
3 changed files with 43 additions and 42 deletions

View File

@ -13,6 +13,7 @@ Gem::Specification.new do |s|
s.add_development_dependency 'rspec', '~> 2.0.0.beta.22' s.add_development_dependency 'rspec', '~> 2.0.0.beta.22'
s.add_development_dependency 'rails', '~> 3.0.0' s.add_development_dependency 'rails', '~> 3.0.0'
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 '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.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

@ -10,11 +10,11 @@ describe CanCan::ControllerResource do
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
@params.merge!(:action => "show", :id => 123) project = Project.create!
stub(Project).find(123) { :some_project } @params.merge!(:action => "show", :id => project.id)
resource = CanCan::ControllerResource.new(@controller) resource = CanCan::ControllerResource.new(@controller)
resource.load_resource resource.load_resource
@controller.instance_variable_get(:@project).should == :some_project @controller.instance_variable_get(:@project).should == project
end end
it "should not load resource into an instance variable if already set" do it "should not load resource into an instance variable if already set" do
@ -26,19 +26,19 @@ describe CanCan::ControllerResource do
end end
it "should properly load resource for namespaced controller" do it "should properly load resource for namespaced controller" do
@params.merge!(:controller => "admin/projects", :action => "show", :id => 123) project = Project.create!
stub(Project).find(123) { :some_project } @params.merge!(:controller => "admin/projects", :action => "show", :id => project.id)
resource = CanCan::ControllerResource.new(@controller) resource = CanCan::ControllerResource.new(@controller)
resource.load_resource resource.load_resource
@controller.instance_variable_get(:@project).should == :some_project @controller.instance_variable_get(:@project).should == project
end end
it "should properly load resource for namespaced controller when using '::' for namespace" do it "should properly load resource for namespaced controller when using '::' for namespace" do
@params.merge!(:controller => "Admin::ProjectsController", :action => "show", :id => 123) project = Project.create!
stub(Project).find(123) { :some_project } @params.merge!(:controller => "Admin::ProjectsController", :action => "show", :id => project.id)
resource = CanCan::ControllerResource.new(@controller) resource = CanCan::ControllerResource.new(@controller)
resource.load_resource resource.load_resource
@controller.instance_variable_get(:@project).should == :some_project @controller.instance_variable_get(:@project).should == project
end end
it "should build a new resource with hash if params[:id] is not specified" do it "should build a new resource with hash if params[:id] is not specified" do
@ -157,11 +157,11 @@ describe CanCan::ControllerResource do
end end
it "should load parent resource through proper id parameter" do it "should load parent resource through proper id parameter" do
@params.merge!(:action => "index", :project_id => 123) project = Project.create!
stub(Project).find(123) { :some_project } @params.merge!(:action => "index", :project_id => project.id)
resource = CanCan::ControllerResource.new(@controller, :project, :parent => true) resource = CanCan::ControllerResource.new(@controller, :project, :parent => true)
resource.load_resource resource.load_resource
@controller.instance_variable_get(:@project).should == :some_project @controller.instance_variable_get(:@project).should == project
end end
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
@ -185,16 +185,16 @@ describe CanCan::ControllerResource do
end end
it "should not load through parent resource if instance isn't loaded when shallow" do it "should not load through parent resource if instance isn't loaded when shallow" do
@params.merge!(:action => "show", :id => 123) project = Project.create!
stub(Project).find(123) { :some_project } @params.merge!(:action => "show", :id => project.id)
resource = CanCan::ControllerResource.new(@controller, :through => :category, :shallow => true) resource = CanCan::ControllerResource.new(@controller, :through => :category, :shallow => true)
resource.load_resource resource.load_resource
@controller.instance_variable_get(:@project).should == :some_project @controller.instance_variable_get(:@project).should == project
end end
it "should raise AccessDenied when attempting to load resource through nil" do it "should raise AccessDenied when attempting to load resource through nil" do
@params.merge!(:action => "show", :id => 123) project = Project.create!
stub(Project).find(123) { :some_project } @params.merge!(:action => "show", :id => project.id)
resource = CanCan::ControllerResource.new(@controller, :through => :category) resource = CanCan::ControllerResource.new(@controller, :through => :category)
lambda { lambda {
resource.load_resource resource.load_resource
@ -242,19 +242,19 @@ describe CanCan::ControllerResource do
end end
it "should only authorize :read action on parent resource" do it "should only authorize :read action on parent resource" do
@params.merge!(:action => "new", :project_id => 123) project = Project.create!
stub(Project).find(123) { :some_project } @params.merge!(:action => "new", :project_id => project.id)
stub(@controller).authorize!(:read, :some_project) { raise CanCan::AccessDenied } stub(@controller).authorize!(:read, project) { raise CanCan::AccessDenied }
resource = CanCan::ControllerResource.new(@controller, :project, :parent => true) resource = CanCan::ControllerResource.new(@controller, :project, :parent => true)
lambda { resource.load_and_authorize_resource }.should raise_error(CanCan::AccessDenied) lambda { resource.load_and_authorize_resource }.should raise_error(CanCan::AccessDenied)
end end
it "should load the model using a custom class" do it "should load the model using a custom class" do
@params.merge!(:action => "show", :id => 123) project = Project.create!
stub(Project).find(123) { :some_project } @params.merge!(:action => "show", :id => project.id)
resource = CanCan::ControllerResource.new(@controller, :class => Project) resource = CanCan::ControllerResource.new(@controller, :class => Project)
resource.load_resource resource.load_resource
@controller.instance_variable_get(:@project).should == :some_project @controller.instance_variable_get(:@project).should == project
end end
it "should authorize based on resource name if class is false" do it "should authorize based on resource name if class is false" do
@ -265,20 +265,20 @@ describe CanCan::ControllerResource do
end end
it "should load and authorize using custom instance name" do it "should load and authorize using custom instance name" do
@params.merge!(:action => "show", :id => 123) project = Project.create!
stub(Project).find(123) { :some_project } @params.merge!(:action => "show", :id => project.id)
stub(@controller).authorize!(:show, :some_project) { raise CanCan::AccessDenied } stub(@controller).authorize!(:show, project) { raise CanCan::AccessDenied }
resource = CanCan::ControllerResource.new(@controller, :instance_name => :custom_project) resource = CanCan::ControllerResource.new(@controller, :instance_name => :custom_project)
lambda { resource.load_and_authorize_resource }.should raise_error(CanCan::AccessDenied) lambda { resource.load_and_authorize_resource }.should raise_error(CanCan::AccessDenied)
@controller.instance_variable_get(:@custom_project).should == :some_project @controller.instance_variable_get(:@custom_project).should == project
end end
it "should load resource using custom find_by attribute" do it "should load resource using custom find_by attribute" do
@params.merge!(:action => "show", :id => 123) project = Project.create!(:name => "foo")
stub(Project).find_by_name!(123) { :some_project } @params.merge!(:action => "show", :id => "foo")
resource = CanCan::ControllerResource.new(@controller, :find_by => :name) resource = CanCan::ControllerResource.new(@controller, :find_by => :name)
resource.load_resource resource.load_resource
@controller.instance_variable_get(:@project).should == :some_project @controller.instance_variable_get(:@project).should == project
end end
it "should raise ImplementationRemoved when adding :name option" do it "should raise ImplementationRemoved when adding :name option" do

View File

@ -1,12 +1,17 @@
require 'rubygems' require 'rubygems'
require 'bundler' require 'bundler/setup'
Bundler.require(:default, :test) Bundler.require(:default)
require 'supermodel' # shouldn't Bundler do this already?
require 'active_support/all' require 'active_support/all'
require 'matchers' require 'matchers'
require 'cancan/matchers' require 'cancan/matchers'
RSpec.configure do |config| RSpec.configure do |config|
config.mock_with :rr config.mock_with :rr
config.before(:each) do
Project.delete_all
Category.delete_all
end
end end
class Ability class Ability
@ -16,17 +21,12 @@ class Ability
end end
end end
# Generic class to mimic a model class Category < SuperModel::Base
class Project has_many :projects
attr_accessor :name
def initialize(attributes = {})
@name = attributes[:name]
end end
def attributes=(attributes) class Project < SuperModel::Base
@name = attributes[:name] if attributes[:name] belongs_to :category
end
class << self class << self
protected protected