ruby-on-rails-3 - ruby on rails 教程 - rails 3.0 第 11 章 rspec 在代码 list 11.27 后失败

标签 ruby-on-rails-3

我已经浏览了这本书。我在每一步之后运行“rspec spec/”。

第 11 章未能在 11.27 中添加 app/views/pages/home.html.erb
list 11.30 之后。将 micropost 实例变量添加到 home 操作。
应用程序/ Controller /pages_controller.rb

一切都应该正常工作。它不是。我只是第一次深入研究轨道。因为这是剪切和粘贴代码,所以我没想到会出错。

我将不胜感激任何有助于调试的帮助。如果我剪切和粘贴错误,或者 Michel 的代码示例没有经过全面测试,请务必解决。

这是 rspec 输出:

MPECHNER-MBP:sample_app michael.pechner$ rspec spec/
..FF.................................................. .................................. FF... .

失败:

1) MicropostsController POST 'create' 失败不应创建微博
失败/错误:post :create, :micropost => @attr
Action View ::模板::错误:
当你没想到时,你有一个 nil 对象!
您可能期望 ActiveRecord::Base 的实例。
评估 nil.errors 时发生错误
# ./app/views/shared/_error_messages.html.erb:1:in _app_views_shared__error_messages_html_erb___2864847557408089106_2179824440__3563936241835894680' # ./app/views/shared/_micropost_form.html.erb:2:in阻止 _app_views_shared__micropost_form_html_erb_ 732794408121469484_2179844600__1418926388395345268'
# ./app/views/shared/_micropost_form.html.erb:1:in _app_views_shared__micropost_form_html_erb___732794408121469484_2179844600__1418926388395345268' # ./app/views/pages/home.html.erb:6:in _app_views_pages_home_html_erb
_1781066003698849377_2179862020__1594538359887050056'
# ./app/controllers/microposts_controller.rb:10:in create' # ./spec/controllers/microposts_controller_spec.rb:34:in '中的 block (5个级别)
# ./spec/controllers/microposts_controller_spec.rb:33:in `block (4 levels) in '

2) MicropostsController POST 'create' 失败应该呈现主页
失败/错误:post :create, :micropost => @attr
Action View ::模板::错误:
当你没想到时,你有一个 nil 对象!
您可能期望 ActiveRecord::Base 的实例。
评估 nil.errors 时发生错误
# ./app/views/shared/_error_messages.html.erb:1:in _app_views_shared__error_messages_html_erb___2864847557408089106_2179824440__3563936241835894680' # ./app/views/shared/_micropost_form.html.erb:2:in阻止 _app_views_shared__micropost_form_html_erb_ 732794408121469484_2179844600__1418926388395345268'
# ./app/views/shared/_micropost_form.html.erb:1:in _app_views_shared__micropost_form_html_erb___732794408121469484_2179844600__1418926388395345268' # ./app/views/pages/home.html.erb:6:in _app_views_pages_home_html_erb
_1781066003698849377_2179862020__1594538359887050056'
# ./app/controllers/microposts_controller.rb:10:in create' # ./spec/controllers/microposts_controller_spec.rb:39:in '中的 block (4个级别)

3) 登录时的 LayoutLinks 应该有一个退出链接
失败/错误:访问 root_path
Action View ::模板::错误:
当你没想到时,你有一个 nil 对象!
您可能期望 ActiveRecord::Base 的实例。
评估 nil.errors 时发生错误
# ./app/views/shared/_error_messages.html.erb:1:in _app_views_shared__error_messages_html_erb___2864847557408089106_2179824440__3563936241835894680' # ./app/views/shared/_micropost_form.html.erb:2:in阻止 _app_views_shared__micropost_form_html_erb_ 732794408121469484_2179844600__1418926388395345268'
# ./app/views/shared/_micropost_form.html.erb:1:in _app_views_shared__micropost_form_html_erb___732794408121469484_2179844600__1418926388395345268' # ./app/views/pages/home.html.erb:6:in _app_views_pages_home_html_erb
_1781066003698849377_2179862020__1594538359887050056'
# :10:in synchronize' # ./spec/requests/layout_links_spec.rb:51:in '中的 block (3个级别)

4) 登录时的 LayoutLinks 应该有个人资料链接
失败/错误:访问 root_path
Action View ::模板::错误:
当你没想到时,你有一个 nil 对象!
您可能期望 ActiveRecord::Base 的实例。
评估 nil.errors 时发生错误
# ./app/views/shared/_error_messages.html.erb:1:in _app_views_shared__error_messages_html_erb___2864847557408089106_2179824440__3563936241835894680' # ./app/views/shared/_micropost_form.html.erb:2:in阻止 _app_views_shared__micropost_form_html_erb_ 732794408121469484_2179844600__1418926388395345268'
# ./app/views/shared/_micropost_form.html.erb:1:in _app_views_shared__micropost_form_html_erb___732794408121469484_2179844600__1418926388395345268' # ./app/views/pages/home.html.erb:6:in _app_views_pages_home_html_erb
_1781066003698849377_2179862020__1594538359887050056'
# :10:in synchronize' # ./spec/requests/layout_links_spec.rb:57:in '中的 block (3个级别)

2.04秒完成
103 个例子,4 次失败
MPECHNER-MBP:sample_app michael.pechner$

最佳答案

我在 git hub 上搜索了其他解决此问题的人。
我在 https://github.com/mfmcgran 的帖子中找到了它
“app/views/shared/_error_messages.html.erb”似乎是错误的,我将第一行显示为“if @users.errors.any?”的代码广告它需要是“如果 object.errors.any?”
第 11 章没有提到这需要改变。但在此更改之后,所有 103 rpsec 测试都通过了。

关于ruby-on-rails-3 - ruby on rails 教程 - rails 3.0 第 11 章 rspec 在代码 list 11.27 后失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4848052/

相关文章:

ruby - custom_require.rb :36: in `gem_original_require'

ruby-on-rails - Spork/Autotest 不自动获取更改

ruby-on-rails - 使用 rspec 测试自定义验证器。为什么我会得到 Proc?

ruby-on-rails-3 - 错误 : migrate SQLite to Postgres

ruby-on-rails - 如何在集合中使用 has_key?

ruby-on-rails - 我如何在 Rails 3 中处理来自 Ajax 请求的响应?

ruby - ROR + Ruby Date Format For 2 days before from Today Date 日期

ruby-on-rails - 更改 Rails 中的默认 View 路径?

ruby-on-rails-3 - 访问Mongoid 3主节点

ruby-on-rails - 为什么 Heroku 拒绝这个 git push?