Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 42 lines (37 sloc) 1.701 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
require "spec_helper"

describe CanCan::InheritedResource do
  before(:each) do
    @params = HashWithIndifferentAccess.new(:controller => "projects")
    @controller_class = Class.new
    @controller = @controller_class.new
    @ability = Ability.new(nil)
    stub(@controller).params { @params }
    stub(@controller).current_ability { @ability }
    stub(@controller_class).cancan_skipper { {:authorize => {}, :load => {}} }
  end

  it "show should load resource through @controller.resource" do
    @params[:action] = "show"
    stub(@controller).resource { :project_resource }
    CanCan::InheritedResource.new(@controller).load_resource
    @controller.instance_variable_get(:@project).should == :project_resource
  end

  it "new should load through @controller.build_resource" do
    @params[:action] = "new"
    stub(@controller).build_resource { :project_resource }
    CanCan::InheritedResource.new(@controller).load_resource
    @controller.instance_variable_get(:@project).should == :project_resource
  end

  it "index should load through @controller.parent when parent" do
    @params[:action] = "index"
    stub(@controller).parent { :project_resource }
    CanCan::InheritedResource.new(@controller, :parent => true).load_resource
    @controller.instance_variable_get(:@project).should == :project_resource
  end

  it "index should load through @controller.end_of_association_chain" do
    @params[:action] = "index"
    stub(Project).accessible_by(@ability, :index) { :projects }
    stub(@controller).end_of_association_chain { Project }
    CanCan::InheritedResource.new(@controller).load_resource
    @controller.instance_variable_get(:@projects).should == :projects
  end
end
Something went wrong with that request. Please try again.