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
|
require 'test_helper'
class GreatSchool
end
class Professor
def self.human_name; 'Professor'; end
end
class ProfessorsController < InheritedResources::Base
belongs_to :school, parent_class: GreatSchool, instance_name: :great_school,
finder: :find_by_title!, param: :school_title
end
class CustomizedBelongsToTest < ActionController::TestCase
tests ProfessorsController
def setup
draw_routes do
resources :professors
end
GreatSchool.expects(:find_by_title!).with('nice').returns(mock_school(professors: Professor))
@controller.stubs(:collection_url).returns('/')
end
def teardown
clear_routes
end
def test_expose_the_requested_school_with_chosen_instance_variable_on_index
Professor.stubs(:scoped).returns([mock_professor])
get :index, params: { school_title: 'nice' }
assert_equal mock_school, assigns(:great_school)
end
def test_expose_the_requested_school_with_chosen_instance_variable_on_show
Professor.stubs(:find).returns(mock_professor)
get :show, params: { id: 42, school_title: 'nice' }
assert_equal mock_school, assigns(:great_school)
end
def test_expose_the_requested_school_with_chosen_instance_variable_on_new
Professor.stubs(:build).returns(mock_professor)
get :new, params: { school_title: 'nice' }
assert_equal mock_school, assigns(:great_school)
end
def test_expose_the_requested_school_with_chosen_instance_variable_on_edit
Professor.stubs(:find).returns(mock_professor)
get :edit, params: { id: 42, school_title: 'nice' }
assert_equal mock_school, assigns(:great_school)
end
def test_expose_the_requested_school_with_chosen_instance_variable_on_create
Professor.stubs(:build).returns(mock_professor(save: true))
post :create, params: { school_title: 'nice' }
assert_equal mock_school, assigns(:great_school)
end
def test_expose_the_requested_school_with_chosen_instance_variable_on_update
Professor.stubs(:find).returns(mock_professor(update: true))
put :update, params: { id: 42, school_title: 'nice' }
assert_equal mock_school, assigns(:great_school)
end
def test_expose_the_requested_school_with_chosen_instance_variable_on_destroy
Professor.stubs(:find).returns(mock_professor(destroy: true))
delete :destroy, params: { id: 42, school_title: 'nice' }
assert_equal mock_school, assigns(:great_school)
end
protected
def mock_school(stubs={})
@mock_school ||= mock(stubs)
end
def mock_professor(stubs={})
@mock_professor ||= mock(stubs)
end
end
|