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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
require 'test_helper'
class Pet
extend ActiveModel::Naming
end
class Puppet
extend ActiveModel::Naming
end
class PetsController < InheritedResources::Base
attr_accessor :current_user
def edit
@pet = 'new pet'
edit!
end
protected
def collection
@pets ||= end_of_association_chain.all
end
def begin_of_association_chain
@current_user
end
end
class BeginOfAssociationChainTest < ActionController::TestCase
tests PetsController
def setup
draw_routes do
resources :pets
end
@controller.current_user = mock()
end
def teardown
clear_routes
end
def test_begin_of_association_chain_is_called_on_index
@controller.current_user.expects(:pets).returns(Pet)
Pet.expects(:all).returns(mock_pet)
get :index
assert_response :success
assert_equal 'Index HTML', @response.body.strip
end
def test_begin_of_association_chain_is_called_on_new
@controller.current_user.expects(:pets).returns(Pet)
Pet.expects(:build).returns(mock_pet)
get :new
assert_response :success
assert_equal 'New HTML', @response.body.strip
end
def test_begin_of_association_chain_is_called_on_show
@controller.current_user.expects(:pets).returns(Pet)
Pet.expects(:find).with('47').returns(mock_pet)
get :show, params: { id: '47' }
assert_response :success
assert_equal 'Show HTML', @response.body.strip
end
def test_instance_variable_should_not_be_set_if_already_defined
@controller.current_user.expects(:pets).never
Pet.expects(:find).never
get :edit, params: { id: '47' }
assert_response :success
assert_equal 'new pet', assigns(:pet)
end
def test_model_is_not_initialized_with_nil
@controller.current_user.expects(:pets).returns(Pet)
Pet.expects(:build).with({}).returns(mock_pet)
get :new
assert_equal mock_pet, assigns(:pet)
end
def test_begin_of_association_chain_is_included_in_chain
@controller.current_user.expects(:pets).returns(Pet)
Pet.expects(:build).with({}).returns(mock_pet)
get :new
assert_equal [@controller.current_user], @controller.send(:association_chain)
end
protected
def mock_pet(stubs={})
@mock_pet ||= mock(stubs)
end
end
class PuppetsController < InheritedResources::Base
optional_belongs_to :pet
end
class AssociationChainTest < ActionController::TestCase
tests PuppetsController
def setup
draw_routes do
resources :puppets
end
@controller.stubs(:collection_url).returns('/')
end
def teardown
clear_routes
end
def test_parent_is_added_to_association_chain
Pet.expects(:find).with('37').returns(mock_pet)
mock_pet.expects(:puppets).returns(Puppet)
Puppet.expects(:find).with('42').returns(mock_puppet)
mock_puppet.expects(:destroy)
delete :destroy, params: { id: '42', pet_id: '37' }
assert_equal [mock_pet], @controller.send(:association_chain)
end
def test_parent_is_added_to_association_chain_if_not_available
Puppet.expects(:find).with('42').returns(mock_puppet)
mock_puppet.expects(:destroy)
delete :destroy, params: { id: '42' }
assert_equal [], @controller.send(:association_chain)
end
protected
def mock_pet(stubs={})
@mock_pet ||= mock(stubs)
end
def mock_puppet(stubs={})
@mock_puppet ||= mock(stubs)
end
end
|