学习如何阅读错误和调试Ruby - 从实用Ruby运行代码时测试错误

时间:2011-08-24 17:53:49

标签: ruby-on-rails ruby ruby-on-rails-3

我正在使用Pragmatic Ruby书的最新版本(3?)来帮助我学习Ruby,并且只是做了一个关于发送电子邮件的教程。我运行了刚刚构建的测试并看到了一些错误。我正在尝试调试它们,而不是简单地重新执行我刚才所做的所有代码。不幸的是,我不太了解Rails设置以了解这些错误的含义以及我应该寻找的内容:

  1) Error:
test_order_received(NotifierTest):
ActionView::Template::Error: /PATH_BASE/agileRails/depot/app/views/notifier/order_received.text.erb:1: syntax error, unexpected '=', expecting keyword_end
..._buffer.safe_concat('Dear '); = @order.name ;@output_buffer....
...                               ^
    /PATH_BASE/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.0.9/lib/action_view/template.rb:258:in `module_eval'
    /PATH_BASE/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.0.9/lib/action_view/template.rb:258:in `compile'
    /PATH_BASE/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.0.9/lib/action_view/template.rb:134:in `block in render'
    /PATH_BASE/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.0.9/lib/active_support/notifications.rb:54:in `instrument'
    /PATH_BASE/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.0.9/lib/action_view/template.rb:127:in `render'
    /PATH_BASE/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.0.9/lib/action_view/render/rendering.rb:59:in `block in _render_template'
    /PATH_BASE/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.0.9/lib/active_support/notifications.rb:54:in `instrument'
    /PATH_BASE/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.0.9/lib/action_view/render/rendering.rb:56:in `_render_template'
    /PATH_BASE/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.0.9/lib/action_view/render/rendering.rb:26:in `render'
    /PATH_BASE/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.0.9/lib/abstract_controller/rendering.rb:115:in `_render_template'
    /PATH_BASE/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.0.9/lib/abstract_controller/rendering.rb:109:in `render_to_body'
    /PATH_BASE/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.0.9/lib/abstract_controller/rendering.rb:102:in `render_to_string'
    /PATH_BASE/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.0.9/lib/abstract_controller/rendering.rb:93:in `render'
    /PATH_BASE/.rvm/gems/ruby-1.9.2-p290/gems/actionmailer-3.0.9/lib/action_mailer/deprecated_api.rb:111:in `render'
    /PATH_BASE/.rvm/gems/ruby-1.9.2-p290/gems/actionmailer-3.0.9/lib/action_mailer/base.rb:735:in `block in collect_responses_and_parts_order'
    /PATH_BASE/.rvm/gems/ruby-1.9.2-p290/gems/actionmailer-3.0.9/lib/action_mailer/base.rb:750:in `each'
    /PATH_BASE/.rvm/gems/ruby-1.9.2-p290/gems/actionmailer-3.0.9/lib/action_mailer/base.rb:750:in `block in each_template'
    /PATH_BASE/.rvm/gems/ruby-1.9.2-p290/gems/actionmailer-3.0.9/lib/action_mailer/base.rb:745:in `each'
    /PATH_BASE/.rvm/gems/ruby-1.9.2-p290/gems/actionmailer-3.0.9/lib/action_mailer/base.rb:745:in `each_template'
    /PATH_BASE/.rvm/gems/ruby-1.9.2-p290/gems/actionmailer-3.0.9/lib/action_mailer/base.rb:731:in `collect_responses_and_parts_order'
    /PATH_BASE/.rvm/gems/ruby-1.9.2-p290/gems/actionmailer-3.0.9/lib/action_mailer/base.rb:673:in `mail'
    /PATH_BASE/Dropbox/Development/instruction/agileRails/depot/app/mailers/notifier.rb:12:in `order_received'
    /PATH_BASE/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.0.9/lib/abstract_controller/base.rb:150:in `process_action'
    /PATH_BASE/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.0.9/lib/abstract_controller/base.rb:119:in `process'
    /PATH_BASE/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.0.9/lib/abstract_controller/rendering.rb:41:in `process'
    /PATH_BASE/.rvm/gems/ruby-1.9.2-p290/gems/actionmailer-3.0.9/lib/action_mailer/old_api.rb:75:in `process'
    /PATH_BASE/.rvm/gems/ruby-1.9.2-p290/gems/actionmailer-3.0.9/lib/action_mailer/base.rb:471:in `process'
    /PATH_BASE/.rvm/gems/ruby-1.9.2-p290/gems/actionmailer-3.0.9/lib/action_mailer/base.rb:466:in `initialize'
    /PATH_BASE/.rvm/gems/ruby-1.9.2-p290/gems/actionmailer-3.0.9/lib/action_mailer/base.rb:450:in `new'
    /PATH_BASE/.rvm/gems/ruby-1.9.2-p290/gems/actionmailer-3.0.9/lib/action_mailer/base.rb:450:in `method_missing'
    test/functional/notifier_test.rb:5:in `block in <class:NotifierTest>'

  2) Error:
test_order_shipped(NotifierTest):
ArgumentError: wrong number of arguments (1 for 0)
    /PATH_BASE/agileRails/depot/app/mailers/notifier.rb:20:in `order_shipped'
    /PATH_BASE/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.0.9/lib/abstract_controller/base.rb:150:in `process_action'
    /PATH_BASE/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.0.9/lib/abstract_controller/base.rb:119:in `process'
    /PATH_BASE/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.0.9/lib/abstract_controller/rendering.rb:41:in `process'
    /PATH_BASE/.rvm/gems/ruby-1.9.2-p290/gems/actionmailer-3.0.9/lib/action_mailer/old_api.rb:75:in `process'
    /PATH_BASE/.rvm/gems/ruby-1.9.2-p290/gems/actionmailer-3.0.9/lib/action_mailer/base.rb:471:in `process'
    /PATH_BASE/.rvm/gems/ruby-1.9.2-p290/gems/actionmailer-3.0.9/lib/action_mailer/base.rb:466:in `initialize'
    /PATH_BASE/.rvm/gems/ruby-1.9.2-p290/gems/actionmailer-3.0.9/lib/action_mailer/base.rb:450:in `new'
    /PATH_BASE/.rvm/gems/ruby-1.9.2-p290/gems/actionmailer-3.0.9/lib/action_mailer/base.rb:450:in `method_missing'
    test/functional/notifier_test.rb:13:in `block in <class:NotifierTest>'

更新以添加order_received.text.erb的代码

Dear <% = @order.name %>

Thank you for your recent order from The Pragmatic Store.

You ordered the following items:
<%= render @order.line_items %>

We'll send you a separate e-mail when your order ships.

2 个答案:

答案 0 :(得分:2)

重要的是这些:

syntax error, unexpected '=', expecting keyword_end
..._buffer.safe_concat('Dear '); = @order.name ;@output_buffer....
...                               ^

=之前的那个分号不应该存在,或者=本身不应该存在:)

编辑:这是问题:

<% = @order.name %>

应该是

<%= @order.name %>

答案 1 :(得分:0)

ActionView::Template::Error: /PATH_BASE/agileRails/depot/app/views/notifier/order_received.text.erb:1: syntax error, unexpected '=', expecting keyword_end
..._buffer.safe_concat('Dear '); = @order.name ;@output_buffer....

通常,错误中最有用的部分位于顶部,下面的所有内容都会进一步进入回溯。在此示例中,错误表示您缺少结束关键字,但我建议删除分号。