gpt4 book ai didi

ruby-on-rails - rails : How to test delegated methods?

转载 作者:行者123 更新时间:2023-11-28 20:40:04 25 4
gpt4 key购买 nike

在我 reworked我的 Rails 应用程序在我的测试中遇到了一些我无法弄清楚的错误。我认为通过“委托(delegate)”Rails 甚至在测试时神奇地知道要使用哪些测试数据进行测试,并且我不必再次在固定装置中声明这些委托(delegate)数据(因为我还没有找到任何关于它的信息)。因为我使用的是脚手架测试,所以我认为它们至少应该无缝地工作——可能是一个笨拙的想法……

也许你有想法?提前致谢!

这里是模型:

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

Controller :

# GET /boxes
def index
@boxes = Box.all.order(box_name: :asc)
end

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>

固定装置:

# box
one:
box_name: MyString
practice_id: 1

two:
box_name: MyString
practice_id: 1

# practice
one:
prac_name: MyString

two:
prac_name: MyString

这里是测试:

test 'should get index' do
get :index
assert_response :success
assert_not_nil assigns(:boxes)
end

结果是我从 guard/minitest 收到关于“get index”和“show box”的错误消息。为什么练习为零? (ActionView::Template::Error: Box#prac_name delegated to practice.prac_name, but practice is nil) 不是通过practice fixture声明的吗?显然不是,但为什么?

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>'

谢谢!

最佳答案

在考虑了评论之后,我再次查看了 test.sqlite 并发现了我在固定装置中的错误:我没有显式声明另一个表的 id,而是将其更改为另一个表的名称并引用像这样:

one:
box_name: MyString
practice: one

two:
box_name: MyString
practice: two

现在我的测试通过了。所以这不是我最初假设的“代表”的问题。尽管如此,我还是会看看 factory_girl。

#wrong
practice_id: 1

#right
practice: one

关于ruby-on-rails - rails : How to test delegated methods?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36884670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com