After I've reworked my rails app I ran in some errors in my tests which I can not figure out. I thought that through "delegate" Rails magically knows even in testing which test data is to use, to test and that I don't have to declare those delegated data in the fixtures again (since I haven't found anything about it). Since I'm using the scaffolded tests I assumed that they should at least work seamlessly - probably a nooby idea...
Maybe you have an idea? Thanks in advance!
Here the models:
class Box < ActiveRecord::Base
belongs_to :practice
delegate :prac_name, to: :practice
has_many :students
validates :practice_id, presence: true
validates :box_name, presence: true, length: { in: 2..20 }
end
class Practice < ActiveRecord::Base
has_many :boxes
validates :prac_name, presence: true, length: { in: 5..50 }
end
The controller:
# GET /boxes
def index
@boxes = Box.all.order(box_name: :asc)
end
The view:
<tbody>
<% @boxes.each do |box| %>
<tr>
<td><%= box.box_name %></td>
<td><%= box.prac_name %></td>
<td><%= link_to 'Show', box %></td>
<td><%= link_to 'Edit', edit_box_path(box) %></td>
<td><%= link_to 'Delete', box, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
The fixtures:
# box
one:
box_name: MyString
practice_id: 1
two:
box_name: MyString
practice_id: 1
# practice
one:
prac_name: MyString
two:
prac_name: MyString
And here the test:
test 'should get index' do
get :index
assert_response :success
assert_not_nil assigns(:boxes)
end
The result is that I get this error message from guard/minitest for "get index" and "show box". Why is practice nil? (ActionView::Template::Error: Box#prac_name delegated to practice.prac_name, but practice is nil) Isn't it declared through the practice fixture? Apparently not but why?
ERROR["test_should_get_index", BoxesControllerTest, 3.196711939992383]
test_should_get_index#BoxesControllerTest (3.20s)
ActionView::Template::Error: ActionView::Template::Error: Box#prac_name delegated to practice.prac_name, but practice is nil: #<Box id: 298486374, box_name: "MyString", created_at: "2016-04-27 07:54:37", updated_at: "2016-04-27 07:54:37", practice_id: 1>
app/models/box.rb:3:in `rescue in prac_name'
app/models/box.rb:3:in `prac_name'
app/views/boxes/index.html.erb:17:in `block in _app_views_boxes_index_html_erb__3226877961716035159_70232972217220'
activerecord (4.2.6) lib/active_record/relation/delegation.rb:46:in `each'
activerecord (4.2.6) lib/active_record/relation/delegation.rb:46:in `each'
app/views/boxes/index.html.erb:14:in `_app_views_boxes_index_html_erb__3226877961716035159_70232972217220'
actionview (4.2.6) lib/action_view/template.rb:145:in `block in render'
activesupport (4.2.6) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.6) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.6) lib/active_support/notifications.rb:164:in `instrument'
actionview (4.2.6) lib/action_view/template.rb:333:in `instrument'
actionview (4.2.6) lib/action_view/template.rb:143:in `render'
actionview (4.2.6) lib/action_view/renderer/template_renderer.rb:54:in `block (2 levels) in render_template'
actionview (4.2.6) lib/action_view/renderer/abstract_renderer.rb:39:in `block in instrument'
activesupport (4.2.6) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.6) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.6) lib/active_support/notifications.rb:164:in `instrument'
actionview (4.2.6) lib/action_view/renderer/abstract_renderer.rb:39:in `instrument'
actionview (4.2.6) lib/action_view/renderer/template_renderer.rb:53:in `block in render_template'
actionview (4.2.6) lib/action_view/renderer/template_renderer.rb:61:in `render_with_layout'
actionview (4.2.6) lib/action_view/renderer/template_renderer.rb:52:in `render_template'
actionview (4.2.6) lib/action_view/renderer/template_renderer.rb:14:in `render'
actionview (4.2.6) lib/action_view/renderer/renderer.rb:46:in `render_template'
actionview (4.2.6) lib/action_view/renderer/renderer.rb:27:in `render'
actionview (4.2.6) lib/action_view/rendering.rb:100:in `_render_template'
actionpack (4.2.6) lib/action_controller/metal/streaming.rb:217:in `_render_template'
actionview (4.2.6) lib/action_view/rendering.rb:83:in `render_to_body'
actionpack (4.2.6) lib/action_controller/metal/rendering.rb:32:in `render_to_body'
actionpack (4.2.6) lib/action_controller/metal/renderers.rb:37:in `render_to_body'
actionpack (4.2.6) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (4.2.6) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (4.2.6) lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'
activesupport (4.2.6) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
/Users/andy/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/benchmark.rb:303:in `realtime'
activesupport (4.2.6) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (4.2.6) lib/action_controller/metal/instrumentation.rb:44:in `block in render'
actionpack (4.2.6) lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'
activerecord (4.2.6) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
actionpack (4.2.6) lib/action_controller/metal/instrumentation.rb:43:in `render'
actionpack (4.2.6) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (4.2.6) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
actionpack (4.2.6) lib/abstract_controller/base.rb:198:in `process_action'
actionpack (4.2.6) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.2.6) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.2.6) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.6) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.6) lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
activesupport (4.2.6) lib/active_support/callbacks.rb:505:in `call'
activesupport (4.2.6) lib/active_support/callbacks.rb:505:in `call'
activesupport (4.2.6) lib/active_support/callbacks.rb:92:in `__run_callbacks__'
activesupport (4.2.6) lib/active_support/callbacks.rb:778:in `_run_process_action_callbacks'
activesupport (4.2.6) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.6) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.2.6) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.2.6) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (4.2.6) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.6) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.6) lib/active_support/notifications.rb:164:in `instrument'
actionpack (4.2.6) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.2.6) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.2.6) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.2.6) lib/abstract_controller/base.rb:137:in `process'
actionview (4.2.6) lib/action_view/rendering.rb:30:in `process'
actionpack (4.2.6) lib/action_controller/test_case.rb:639:in `process'
actionpack (4.2.6) lib/action_controller/test_case.rb:67:in `process'
actionpack (4.2.6) lib/action_controller/test_case.rb:514:in `get'
test/controllers/boxes_controller_test.rb:9:in `block in <class:BoxesControllerTest>'
minitest (5.8.4) lib/minitest/test.rb:108:in `block (3 levels) in run'
minitest (5.8.4) lib/minitest/test.rb:205:in `capture_exceptions'
minitest (5.8.4) lib/minitest/test.rb:105:in `block (2 levels) in run'
minitest (5.8.4) lib/minitest/test.rb:256:in `time_it'
minitest (5.8.4) lib/minitest/test.rb:104:in `block in run'
minitest (5.8.4) lib/minitest.rb:331:in `on_signal'
minitest (5.8.4) lib/minitest/test.rb:276:in `with_info_handler'
minitest (5.8.4) lib/minitest/test.rb:103:in `run'
minitest-reporters (1.1.8) lib/minitest/reporters.rb:48:in `run_with_hooks'
minitest (5.8.4) lib/minitest.rb:778:in `run_one_method'
minitest (5.8.4) lib/minitest.rb:305:in `run_one_method'
minitest (5.8.4) lib/minitest.rb:293:in `block (2 levels) in run'
minitest (5.8.4) lib/minitest.rb:292:in `each'
minitest (5.8.4) lib/minitest.rb:292:in `block in run'
minitest (5.8.4) lib/minitest.rb:331:in `on_signal'
minitest (5.8.4) lib/minitest.rb:318:in `with_info_handler'
minitest (5.8.4) lib/minitest.rb:291:in `run'
minitest (5.8.4) lib/minitest.rb:152:in `block in __run'
minitest (5.8.4) lib/minitest.rb:152:in `map'
minitest (5.8.4) lib/minitest.rb:152:in `__run'
minitest (5.8.4) lib/minitest.rb:129:in `run'
minitest (5.8.4) lib/minitest.rb:56:in `block in autorun'
/Users/andy/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/Users/andy/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
-e:1:in `<main>'
Thanks!
Aucun commentaire:
Enregistrer un commentaire