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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
require 'test_helper'
class Subfaculty
end
class Speciality
end
module Plan
class Group
end
class Education
end
end
class GroupsController < InheritedResources::Base
defaults resource_class: Plan::Group, finder: :find_by_slug
belongs_to :subfaculty, shallow: true do
belongs_to :speciality
end
end
class EducationsController < InheritedResources::Base
defaults resource_class: Plan::Education
belongs_to :subfaculty, shallow: true do
belongs_to :speciality do
belongs_to :group, parent_class: Plan::Group,
instance_name: :plan_group,
param: :group_id,
finder: :find_by_slug
end
end
end
class NestedModelWithShallowTest < ActionController::TestCase
tests GroupsController
def setup
draw_routes do
resources :groups
end
mock_speciality.expects(:subfaculty).returns(mock_subfaculty)
mock_subfaculty.expects(:to_param).returns('13')
Subfaculty.expects(:find).with('13').returns(mock_subfaculty)
mock_subfaculty.expects(:specialities).returns(Speciality)
mock_speciality.expects(:groups).returns(Plan::Group)
end
def teardown
clear_routes
end
def test_assigns_subfaculty_and_speciality_and_group_on_edit
should_find_parents
get :edit, params: { id: 'forty_two' }
assert_equal mock_subfaculty, assigns(:subfaculty)
assert_equal mock_speciality, assigns(:speciality)
assert_equal mock_group, assigns(:group)
end
def test_expose_a_newly_create_group_with_speciality
Speciality.expects(:find).with('37').twice.returns(mock_speciality)
Plan::Group.expects(:build).with({'these' => 'params'}).returns(mock_group(save: true))
post :create, params: { speciality_id: '37', group: {'these' => 'params'} }
assert_equal mock_group, assigns(:group)
end
def test_expose_a_update_group_with_speciality
should_find_parents
mock_group.expects(:update).with('these' => 'params').returns(true)
post :update, params: { id: 'forty_two', group: {'these' => 'params'} }
assert_equal mock_group, assigns(:group)
end
protected
def should_find_parents
Plan::Group.expects(:find_by_slug).with('forty_two').returns(mock_group)
mock_group.expects(:speciality).returns(mock_speciality)
mock_speciality.expects(:to_param).returns('37')
Plan::Group.expects(:find_by_slug).with('forty_two').returns(mock_group)
Speciality.expects(:find).with('37').returns(mock_speciality)
end
def mock_group(stubs={})
@mock_group ||= mock(stubs)
end
def mock_speciality(stubs={})
@mock_speciality ||= mock(stubs)
end
def mock_subfaculty(stubs={})
@mock_subfaculty ||= mock(stubs)
end
end
class TwoNestedModelWithShallowTest < ActionController::TestCase
tests EducationsController
def setup
draw_routes do
resources :educations
end
mock_speciality.expects(:subfaculty).returns(mock_subfaculty)
mock_subfaculty.expects(:to_param).returns('13')
Subfaculty.expects(:find).with('13').returns(mock_subfaculty)
mock_subfaculty.expects(:specialities).returns(Speciality)
mock_speciality.expects(:groups).returns(Plan::Group)
end
def teardown
clear_routes
end
def test_assigns_subfaculty_and_speciality_and_group_on_new
should_find_parents
get :new, params: { group_id: 'forty_two' }
assert_equal mock_subfaculty, assigns(:subfaculty)
assert_equal mock_speciality, assigns(:speciality)
assert_equal mock_group, assigns(:plan_group)
assert_equal mock_education, assigns(:education)
end
protected
def should_find_parents
Plan::Group.expects(:find_by_slug).with('forty_two').returns(mock_group)
mock_group.expects(:speciality).returns(mock_speciality)
mock_group.expects(:educations).returns(mock_education)
mock_education.expects(:build).returns(mock_education)
mock_speciality.expects(:to_param).returns('37')
Plan::Group.expects(:find_by_slug).with('forty_two').returns(mock_group)
Speciality.expects(:find).with('37').returns(mock_speciality)
end
def mock_group(stubs={})
@mock_group ||= mock(stubs)
end
def mock_education(stubs={})
@mock_education ||= mock(stubs)
end
def mock_speciality(stubs={})
@mock_speciality ||= mock(stubs)
end
def mock_subfaculty(stubs={})
@mock_subfaculty ||= mock(stubs)
end
end
|