ruby on rails 教程 - rails 3.0 第 11 章 rspec 在代码清单 11.27 后失败 [英] ruby on rails tutorial - rails 3.0 chapter 11 rspec fails after code listing 11.27
问题描述
我已经看完了这本书.我在每一步之后运行rspec spec/".
I have run through the book. I run "rspec spec/" after each step.
在11.27中添加app/views/pages/home.html.erb开始11章失败清单 11.30 之后.将 micropost 实例变量添加到 home 操作.应用程序/控制器/pages_controller.rb
Chapter 11 fails starting listin 11.27 adding app/views/pages/home.html.erb After Listing 11.30. Adding a micropost instance variable to the home action. app/controllers/pages_controller.rb
一切正常.不是.我只是第一次研究 Rails.由于这是剪切和粘贴代码,我没想到会出错.
Everything is supposed to be working. It isn't. I am just digging into rails for the very first time. since this was cut and paste code, I did not expect errors.
我将非常感谢您对调试此问题的任何帮助.如果我剪切和粘贴错误,或者 Michel 的代码示例没有经过全面测试,请确认.
I will be grateful for any help in leaning to debug this. Firure out if I cut and paste wrong, or if Michel's code example is not fully tested.
这是 rspec 输出:
Here is the rspec output:
MPECHNER-MBP:sample_app michael.pechner$ rspec 规范/……FF……………………………………………………...................................... FF....
MPECHNER-MBP:sample_app michael.pechner$ rspec spec/ ..FF.............................................................................................FF....
失败:
1) MicropostsController POST 'create' 失败不应创建微博失败/错误:post :create, :micropost => @attr动作视图::模板::错误:当你没想到的时候,你有一个 nil 对象!您可能希望有一个 ActiveRecord::Base 实例.评估 nil.errors 时发生错误# ./app/views/shared/_error_messages.html.erb:1:in _app_views_shared__error_messages_html_erb___2864847557408089106_2179824440__356393624183589460# ./app/views/shared/_micropost_form.html.erb:2:in
block in _app_views_shared__micropost_form_html_erb_732794408121469484_2179844600__1418926385826983'# ./app/views/shared/_micropost_form.html.erb:1:in _app_views_shared__micropost_form_html_erb___732794408121469484_2179844600__1418926388398534526# ./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 级) in '# ./spec/controllers/microposts_controller_spec.rb:33:in `block (4 levels) in '
1) MicropostsController POST 'create' failure should not create a micropost
Failure/Error: post :create, :micropost => @attr
ActionView::Template::Error:
You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating 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
block 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 levels) in '
# ./spec/controllers/microposts_controller_spec.rb:33:in `block (4 levels) in '
2) MicropostsController POST 'create' 失败应该呈现主页失败/错误:post :create, :micropost => @attr动作视图::模板::错误:当你没想到的时候,你有一个 nil 对象!您可能期望有一个 ActiveRecord::Base 实例.评估 nil.errors 时发生错误# ./app/views/shared/_error_messages.html.erb:1:in _app_views_shared__error_messages_html_erb___2864847557408089106_2179824440__356393624183589460# ./app/views/shared/_micropost_form.html.erb:2:in
block in _app_views_shared__micropost_form_html_erb_732794408121469484_2179844600__1418926385826983'# ./app/views/shared/_micropost_form.html.erb:1:in _app_views_shared__micropost_form_html_erb___732794408121469484_2179844600__1418926388398534526# ./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 levels) in '
2) MicropostsController POST 'create' failure should render the home page
Failure/Error: post :create, :micropost => @attr
ActionView::Template::Error:
You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating 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
block 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 levels) in '
3) 登录时的 LayoutLinks 应该有一个退出链接失败/错误:访问 root_path动作视图::模板::错误:当你没想到的时候,你有一个 nil 对象!您可能期望有一个 ActiveRecord::Base 实例.评估 nil.errors 时发生错误# ./app/views/shared/_error_messages.html.erb:1:in _app_views_shared__error_messages_html_erb___2864847557408089106_2179824440__356393624183589460# ./app/views/shared/_micropost_form.html.erb:2:in
block in _app_views_shared__micropost_form_html_erb_732794408121469484_2179844600__1418926385826983'# ./app/views/shared/_micropost_form.html.erb:1:in _app_views_shared__micropost_form_html_erb___732794408121469484_2179844600__1418926388398534526# ./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 levels) in '
3) LayoutLinks when signed in should have a signout link
Failure/Error: visit root_path
ActionView::Template::Error:
You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating 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
block 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 levels) in '
4) 登录时的 LayoutLinks 应该有个人资料链接失败/错误:访问 root_path动作视图::模板::错误:当你没想到的时候,你有一个 nil 对象!您可能期望有一个 ActiveRecord::Base 实例.评估 nil.errors 时发生错误# ./app/views/shared/_error_messages.html.erb:1:in _app_views_shared__error_messages_html_erb___2864847557408089106_2179824440__356393624183589460# ./app/views/shared/_micropost_form.html.erb:2:in
block in _app_views_shared__micropost_form_html_erb_732794408121469484_2179844600__1418926385826953# ./app/views/shared/_micropost_form.html.erb:1:in _app_views_shared__micropost_form_html_erb___732794408121469484_2179844600__1418926388398534526# ./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
块(3级)在'
4) LayoutLinks when signed in should have a profile link
Failure/Error: visit root_path
ActionView::Template::Error:
You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating 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
block 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 levels) in '
在 2.04 秒内完成103 个例子,4 个失败MPECHNER-MBP:sample_app michael.pechner$
Finished in 2.04 seconds 103 examples, 4 failures MPECHNER-MBP:sample_app michael.pechner$
推荐答案
我在 git hub 上搜索了其他解决过这个问题的人.我在 https://github.com/mfmcgran 的帖子中找到了它app/views/shared/_error_messages.html.erb"似乎是错误的,我在第一行显示的代码为if @users.errors.any?"广告它需要是if object.errors.any?"第 11 章没有提到这需要改变.但是在这个改变之后,所有 103 个 rpsec 测试都通过了.
I searched git hub for others that have worked through this. I found it in the posting by https://github.com/mfmcgran "app/views/shared/_error_messages.html.erb" seems to be wrong, The code I had showed the first line as "if @users.errors.any?" ad it needs to be "if object.errors.any?" There was no mention in chapter 11 that this needed to changed. But after this change, all 103 rpsec tests passed.
这篇关于ruby on rails 教程 - rails 3.0 第 11 章 rspec 在代码清单 11.27 后失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!