test/unit/trust/controller/resource_test.rb in trust-0.8.3 vs test/unit/trust/controller/resource_test.rb in trust-1.4.2

- old
+ new

@@ -138,24 +138,24 @@ @request = Object.new @resources = [NameSpacedResource::Person, :child] end context 'when found' do should 'return object for namespaced resource' do - @request.stubs(:symbolized_path_parameters).returns({:name_spaced_resource_person_id => 2 }) + @request.stubs(:path_parameters).returns({:name_spaced_resource_person_id => 2 }) NameSpacedResource::Person.expects(:find).with(2).returns(@object = NameSpacedResource::Person.new) @res = Trust::Controller::Resource::ParentInfo.new(@resources, {}, @request) assert_equal @object, @res.object end should 'return object for regular resource' do - @request.stubs(:symbolized_path_parameters).returns({:child_id => 2 }) + @request.stubs(:path_parameters).returns({:child_id => 2 }) Child.expects(:find).with(2).returns(@object = Child.new) @res = Trust::Controller::Resource::ParentInfo.new(@resources, {}, @request) assert_equal @object, @res.object end context 'the attributes' do setup do - @request.stubs(:symbolized_path_parameters).returns({:child_id => 2 }) + @request.stubs(:path_parameters).returns({:child_id => 2 }) Child.expects(:find).with(2).returns(@object = Child.new) @res = Trust::Controller::Resource::ParentInfo.new(@resources, {:child => 'tie'}, @request) end should 'return class for object' do assert_equal @object, @res.object @@ -171,34 +171,34 @@ assert_equal 'tie', @res.params end end end should 'return nil for object if not found' do - @request.stubs(:symbolized_path_parameters).returns({:child_id => 2 }) + @request.stubs(:path_parameters).returns({:child_id => 2 }) Child.expects(:find).with(2).returns(nil) @res = Trust::Controller::Resource::ParentInfo.new(@resources, {}, @request) assert_nil @res.object assert !@res.object? end should 'return nil for object if not specified' do - @request.stubs(:symbolized_path_parameters).returns({}) + @request.stubs(:path_parameters).returns({}) @res = Trust::Controller::Resource::ParentInfo.new(@resources, {}, @request) assert_nil @res.object assert !@res.object? end should 'return nil for klass when not found' do - @request.stubs(:symbolized_path_parameters).returns({}) + @request.stubs(:path_parameters).returns({}) @res = Trust::Controller::Resource::ParentInfo.new(@resources, {}, @request) assert_nil @res.klass end end context 'Parent resource with inheritance' do setup do @request = Object.new @resources = [:parent] - @request.stubs(:symbolized_path_parameters).returns({:child_id => 2 }) + @request.stubs(:path_parameters).returns({:child_id => 2 }) Parent.expects(:find).with(2).returns(@object = Child.new) @res = Trust::Controller::Resource::ParentInfo.new(@resources, {}, @request) end should 'resolve descendants' do assert_equal @object, @res.object @@ -250,10 +250,18 @@ assert_equal 1, @resource.instantiated @resource.expects(:instances).returns(nil) @resource.expects(:instance).returns(2) assert_equal 2, @resource.instantiated end + should 'provide access to nested' do + @resource.expects(:parent).twice.returns(:parent) + @resource.expects(:instance).returns(:instance) + assert_equal [:parent, :instance], @resource.nested + @resource.expects(:parent).returns(nil) + @resource.expects(:instance).returns(:instance) + assert_equal :instance, *@resource.nested + end should 'provide collection' do @resource_info.expects(:collection).with(@parent_info, nil).returns(1) assert_equal 1, @resource.collection @resource_info.expects(:collection).with(@parent_info, 2).returns(3) assert_equal 3, @resource.collection(2) @@ -266,25 +274,37 @@ assert_equal 6, @resource.parent assert @controller.instance_variable_get(:@child).is_a?(Child) assert @resource.instance.is_a?(Child) end end - context 'Member actions' do + context 'Actions' do setup do Trust::Controller::Resource.any_instance.expects(:extract_resource_info).with('child', { :id => 1 }).returns(@resource_info) Trust::Controller::Resource.any_instance.expects(:extract_parent_info).with({:parent => nil}, { :id => 1 }, @request).returns(@parent_info) end - should 'load as expected' do + should 'load member as expected' do @resource = Trust::Controller::Resource.new(@controller, @properties, 'member',{ :id => 1 }, @request) @properties.actions :member => [:member] @resource_info.stubs(:params).returns({}) @controller.expects(:respond_to?).with(:build,true).returns(false) Child.expects(:find).with(1).returns(Child.new) @resource.load assert_equal 6, @controller.instance_variable_get(:@parent) assert_equal 6, @resource.parent assert @controller.instance_variable_get(:@child).is_a?(Child) assert @resource.instance.is_a?(Child) + end + should 'discovered collection_action? as a method' do + @resource = Trust::Controller::Resource.new(@controller, @properties, 'index',{ :id => 1 }, @request) + assert @resource.collection_action? + end + should 'discovered member_action? as a method' do + @resource = Trust::Controller::Resource.new(@controller, @properties, 'show',{ :id => 1 }, @request) + assert @resource.member_action? + end + should 'discovered new_action? as a method' do + @resource = Trust::Controller::Resource.new(@controller, @properties, 'new',{ :id => 1 }, @request) + assert @resource.new_action? end end context 'Nested resources' do setup do Trust::Controller::Resource.any_instance.expects(:extract_resource_info).with('child', { :child_id => 1 }).returns(@resource_info)