2
votes

I have run through the book. I run "rspec spec/" after each step.

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

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.

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.

Here is the rspec output:

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

Failures:

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:inblock 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:inblock (5 levels) in ' # ./spec/controllers/microposts_controller_spec.rb:33: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:inblock 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:inblock (4 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:inblock 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:inblock (3 levels) in '

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:inblock 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:inblock (3 levels) in '

Finished in 2.04 seconds 103 examples, 4 failures MPECHNER-MBP:sample_app michael.pechner$

2

2 Answers

8
votes

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.

0
votes

The tutorial has gotten a little older (given the speed at which gem versions get released). I find its really convenient if you just upgrade to the most recent version of rspec and use capybara instead

gem 'capybara'

in your Gemfile and

gem 'capybara/rspec'

in your spec_helper.rb and run

bundle install

this should then work

click_link 'About'
page.should have_content("About")
# in place of
# enter code here`response.should have_selector('title', :content => "Contact")

this will continue to work as well

get '/about'
response.should have_selector('title', :content => "About")