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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
|
require 'test_helper'
class Malarz
def self.human_name; 'Painter'; end
def to_param
self.slug
end
end
class PaintersController < InheritedResources::Base
defaults instance_name: 'malarz', collection_name: 'malarze',
resource_class: Malarz, route_prefix: nil,
finder: :find_by_slug
end
class DefaultsTest < ActionController::TestCase
tests PaintersController
def setup
draw_routes do
resources :painters
end
end
def teardown
clear_routes
end
def test_expose_all_painters_as_instance_variable
Malarz.expects(:scoped).returns([mock_painter])
get :index
assert_equal [mock_painter], assigns(:malarze)
end
def test_expose_the_requested_painter_on_show
Malarz.expects(:find_by_slug).with('forty_two').returns(mock_painter)
get :show, params: { id: 'forty_two' }
assert_equal mock_painter, assigns(:malarz)
end
def test_expose_a_new_painter
Malarz.expects(:new).returns(mock_painter)
get :new
assert_equal mock_painter, assigns(:malarz)
end
def test_expose_the_requested_painter_on_edit
Malarz.expects(:find_by_slug).with('forty_two').returns(mock_painter)
get :edit, params: { id: 'forty_two' }
assert_response :success
assert_equal mock_painter, assigns(:malarz)
end
def test_expose_a_newly_create_painter_when_saved_with_success
Malarz.expects(:new).with({'these' => 'params'}).returns(mock_painter(save: true))
post :create, params: { malarz: {these: 'params'} }
assert_equal mock_painter, assigns(:malarz)
end
def test_update_the_requested_object
Malarz.expects(:find_by_slug).with('forty_two').returns(mock_painter)
mock_painter.expects(:update).with({'these' => 'params'}).returns(true)
put :update, params: { id: 'forty_two', malarz: {these: 'params'} }
assert_equal mock_painter, assigns(:malarz)
end
def test_the_requested_painter_is_destroyed
Malarz.expects(:find_by_slug).with('forty_two').returns(mock_painter)
mock_painter.expects(:destroy)
delete :destroy, params: { id: 'forty_two' }
assert_equal mock_painter, assigns(:malarz)
end
protected
def mock_painter(stubs={})
@mock_painter ||= mock(stubs)
end
end
class Lecturer
def self.human_name; 'Einstein'; end
end
module University; end
class University::LecturersController < InheritedResources::Base
defaults finder: :find_by_slug
end
class DefaultsNamespaceTest < ActionController::TestCase
tests University::LecturersController
def setup
draw_routes do
namespace :university do
resources :lecturers
end
end
end
def teardown
clear_routes
end
def test_expose_all_lecturers_as_instance_variable
Lecturer.expects(:scoped).returns([mock_lecturer])
get :index
assert_equal [mock_lecturer], assigns(:lecturers)
end
def test_expose_the_requested_lecturer_on_show
Lecturer.expects(:find_by_slug).with('forty_two').returns(mock_lecturer)
get :show, params: { id: 'forty_two' }
assert_equal mock_lecturer, assigns(:lecturer)
end
def test_expose_a_new_lecturer
Lecturer.expects(:new).returns(mock_lecturer)
get :new
assert_equal mock_lecturer, assigns(:lecturer)
end
def test_expose_the_requested_lecturer_on_edit
Lecturer.expects(:find_by_slug).with('forty_two').returns(mock_lecturer)
get :edit, params: { id: 'forty_two' }
assert_response :success
assert_equal mock_lecturer, assigns(:lecturer)
end
def test_expose_a_newly_create_lecturer_when_saved_with_success
Lecturer.expects(:new).with({'these' => 'params'}).returns(mock_lecturer(save: true))
post :create, params: { lecturer: {these: 'params'} }
assert_equal mock_lecturer, assigns(:lecturer)
end
def test_update_the_lecturer
Lecturer.expects(:find_by_slug).with('forty_two').returns(mock_lecturer)
mock_lecturer.expects(:update).with({'these' => 'params'}).returns(true)
put :update, params: { id: 'forty_two', lecturer: {these: 'params'} }
assert_equal mock_lecturer, assigns(:lecturer)
end
def test_the_requested_lecturer_is_destroyed
Lecturer.expects(:find_by_slug).with('forty_two').returns(mock_lecturer)
mock_lecturer.expects(:destroy)
delete :destroy, params: { id: 'forty_two' }
assert_equal mock_lecturer, assigns(:lecturer)
end
protected
def mock_lecturer(stubs={})
@mock_lecturer ||= mock(stubs)
end
end
class Group
end
class AdminGroup
end
module Admin; end
class Admin::Group
end
class Admin::GroupsController < InheritedResources::Base
end
class NamespacedModelForNamespacedController < ActionController::TestCase
tests Admin::GroupsController
def test_that_it_picked_the_namespaced_model
# make public so we can test it
Admin::GroupsController.send(:public, :resource_class)
assert_equal Admin::Group, @controller.resource_class
end
end
class Role
end
class AdminRole
end
class Admin::RolesController < InheritedResources::Base
end
class TwoPartNameModelForNamespacedController < ActionController::TestCase
tests Admin::RolesController
def test_that_it_picked_the_camelcased_model
# make public so we can test it
Admin::RolesController.send(:public, :resource_class)
assert_equal AdminRole, @controller.resource_class
end
end
class User
end
class Admin::UsersController < InheritedResources::Base
end
class AnotherTwoPartNameModelForNamespacedController < ActionController::TestCase
tests Admin::UsersController
def test_that_it_picked_the_camelcased_model
# make public so we can test it
Admin::UsersController.send(:public, :resource_class)
assert_equal User, @controller.resource_class
end
def test_that_it_got_the_request_params_right
# make public so we can test it
Admin::UsersController.send(:public, :resources_configuration)
assert_equal 'user', @controller.resources_configuration[:self][:request_name]
end
end
module MyEngine
class Engine < Rails::Engine
isolate_namespace MyEngine
end
class Person
extend ActiveModel::Naming
end
class PeopleController < InheritedResources::Base
defaults resource_class: Person
end
end
class IsolatedEngineModelController < ActionController::TestCase
tests MyEngine::PeopleController
def setup
# make public so we can test it
MyEngine::PeopleController.send(:public, *MyEngine::PeopleController.protected_instance_methods)
end
def test_isolated_model_name
assert_equal 'person', @controller.resources_configuration[:self][:request_name]
end
end
|