Permalink
Browse files

using supermodel in specs to remove some of the model stubs

  • Loading branch information...
1 parent 67cd83d commit f901c367fced88a379f7cb7627990350fec7f24a @ryanb committed Oct 8, 2010
Showing with 43 additions and 42 deletions.
  1. +1 −0 cancan.gemspec
  2. +30 −30 spec/cancan/controller_resource_spec.rb
  3. +12 −12 spec/spec_helper.rb
View
@@ -13,6 +13,7 @@ Gem::Specification.new do |s|
s.add_development_dependency 'rspec', '~> 2.0.0.beta.22'
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 'supermodel', '~> 0.1.4'
s.rubyforge_project = s.name
s.required_rubygems_version = ">= 1.3.4"
@@ -10,11 +10,11 @@
end
it "should load the resource into an instance variable if params[:id] is specified" do
- @params.merge!(:action => "show", :id => 123)
- stub(Project).find(123) { :some_project }
+ project = Project.create!
+ @params.merge!(:action => "show", :id => project.id)
resource = CanCan::ControllerResource.new(@controller)
resource.load_resource
- @controller.instance_variable_get(:@project).should == :some_project
+ @controller.instance_variable_get(:@project).should == project
end
it "should not load resource into an instance variable if already set" do
@@ -26,19 +26,19 @@
end
it "should properly load resource for namespaced controller" do
- @params.merge!(:controller => "admin/projects", :action => "show", :id => 123)
- stub(Project).find(123) { :some_project }
+ project = Project.create!
+ @params.merge!(:controller => "admin/projects", :action => "show", :id => project.id)
resource = CanCan::ControllerResource.new(@controller)
resource.load_resource
- @controller.instance_variable_get(:@project).should == :some_project
+ @controller.instance_variable_get(:@project).should == project
end
it "should properly load resource for namespaced controller when using '::' for namespace" do
- @params.merge!(:controller => "Admin::ProjectsController", :action => "show", :id => 123)
- stub(Project).find(123) { :some_project }
+ project = Project.create!
+ @params.merge!(:controller => "Admin::ProjectsController", :action => "show", :id => project.id)
resource = CanCan::ControllerResource.new(@controller)
resource.load_resource
- @controller.instance_variable_get(:@project).should == :some_project
+ @controller.instance_variable_get(:@project).should == project
end
it "should build a new resource with hash if params[:id] is not specified" do
@@ -157,11 +157,11 @@
end
it "should load parent resource through proper id parameter" do
- @params.merge!(:action => "index", :project_id => 123)
- stub(Project).find(123) { :some_project }
+ project = Project.create!
+ @params.merge!(:action => "index", :project_id => project.id)
resource = CanCan::ControllerResource.new(@controller, :project, :parent => true)
resource.load_resource
- @controller.instance_variable_get(:@project).should == :some_project
+ @controller.instance_variable_get(:@project).should == project
end
it "should load resource through the association of another parent resource using instance variable" do
@@ -185,16 +185,16 @@
end
it "should not load through parent resource if instance isn't loaded when shallow" do
- @params.merge!(:action => "show", :id => 123)
- stub(Project).find(123) { :some_project }
+ project = Project.create!
+ @params.merge!(:action => "show", :id => project.id)
resource = CanCan::ControllerResource.new(@controller, :through => :category, :shallow => true)
resource.load_resource
- @controller.instance_variable_get(:@project).should == :some_project
+ @controller.instance_variable_get(:@project).should == project
end
it "should raise AccessDenied when attempting to load resource through nil" do
- @params.merge!(:action => "show", :id => 123)
- stub(Project).find(123) { :some_project }
+ project = Project.create!
+ @params.merge!(:action => "show", :id => project.id)
resource = CanCan::ControllerResource.new(@controller, :through => :category)
lambda {
resource.load_resource
@@ -242,19 +242,19 @@
end
it "should only authorize :read action on parent resource" do
- @params.merge!(:action => "new", :project_id => 123)
- stub(Project).find(123) { :some_project }
- stub(@controller).authorize!(:read, :some_project) { raise CanCan::AccessDenied }
+ project = Project.create!
+ @params.merge!(:action => "new", :project_id => project.id)
+ stub(@controller).authorize!(:read, project) { raise CanCan::AccessDenied }
resource = CanCan::ControllerResource.new(@controller, :project, :parent => true)
lambda { resource.load_and_authorize_resource }.should raise_error(CanCan::AccessDenied)
end
it "should load the model using a custom class" do
- @params.merge!(:action => "show", :id => 123)
- stub(Project).find(123) { :some_project }
+ project = Project.create!
+ @params.merge!(:action => "show", :id => project.id)
resource = CanCan::ControllerResource.new(@controller, :class => Project)
resource.load_resource
- @controller.instance_variable_get(:@project).should == :some_project
+ @controller.instance_variable_get(:@project).should == project
end
it "should authorize based on resource name if class is false" do
@@ -265,20 +265,20 @@
end
it "should load and authorize using custom instance name" do
- @params.merge!(:action => "show", :id => 123)
- stub(Project).find(123) { :some_project }
- stub(@controller).authorize!(:show, :some_project) { raise CanCan::AccessDenied }
+ project = Project.create!
+ @params.merge!(:action => "show", :id => project.id)
+ stub(@controller).authorize!(:show, project) { raise CanCan::AccessDenied }
resource = CanCan::ControllerResource.new(@controller, :instance_name => :custom_project)
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
it "should load resource using custom find_by attribute" do
- @params.merge!(:action => "show", :id => 123)
- stub(Project).find_by_name!(123) { :some_project }
+ project = Project.create!(:name => "foo")
+ @params.merge!(:action => "show", :id => "foo")
resource = CanCan::ControllerResource.new(@controller, :find_by => :name)
resource.load_resource
- @controller.instance_variable_get(:@project).should == :some_project
+ @controller.instance_variable_get(:@project).should == project
end
it "should raise ImplementationRemoved when adding :name option" do
View
@@ -1,12 +1,17 @@
require 'rubygems'
-require 'bundler'
-Bundler.require(:default, :test)
+require 'bundler/setup'
+Bundler.require(:default)
+require 'supermodel' # shouldn't Bundler do this already?
require 'active_support/all'
require 'matchers'
require 'cancan/matchers'
RSpec.configure do |config|
config.mock_with :rr
+ config.before(:each) do
+ Project.delete_all
+ Category.delete_all
+ end
end
class Ability
@@ -16,17 +21,12 @@ def initialize(user)
end
end
-# Generic class to mimic a model
-class Project
- attr_accessor :name
-
- def initialize(attributes = {})
- @name = attributes[:name]
- end
+class Category < SuperModel::Base
+ has_many :projects
+end
- def attributes=(attributes)
- @name = attributes[:name] if attributes[:name]
- end
+class Project < SuperModel::Base
+ belongs_to :category
class << self
protected

0 comments on commit f901c36

Please sign in to comment.