Note that there are some explanatory texts on larger screens.

plurals
  1. POrspec error in ruby with before each do
    primarykey
    data
    text
    <p>I am working through the Learn Rails By Example book and came across an interesting problem. I'll apologize in advance if I am just doing something wrong. Here is the problem.</p> <p>In section 3.5, the exercise it asks you to do the following:</p> <blockquote> <p>You may have noticed some repetition in the Pages controller spec (Listing 3.20). In particular, the base title, “Ruby on Rails Tutorial Sample App”, is the same for every title test. Using the RSpec before(:each) facility, which executes a block of code before each test case, fill in Listing 3.33 to define a @base_title instance variable that eliminates this duplication. (This code uses two new elements: a symbol, :each, and the string concatenation operator +. We’ll learn more about both in Chapter 4, and we’ll see before(:each) again in Section 6.2.1.) Note that, with the base title captured in an instance variable, we are now able to align :content with the first character inside each left parenthesis (. This is my preferred convention for formatting code broken into multiple lines.</p> </blockquote> <p>Here is how my pages_controller_spec.rb looks:</p> <pre><code>describe PagesController do render_views before(:each) do # Define @base_title here. base_title = "Ruby on Rails Tutorial Sample App" end describe "GET 'home'" do it "should be successful" do get 'home' response.should be_success end it "should have the right title" do get 'home' response.should have_selector("title", :content =&gt; @base_title + " | Home") end end </code></pre> <p>When I load the rails server and open the webpage, everything works perfect. The title shows up as it should per the base_title. However, when I run rspec, I receive the following errors. I'd really like to get this cleared up in rspec. What do you think is wrong?</p> <pre><code> 1) PagesController GET 'home' should have the right title Failure/Error: :content =&gt; @base_title + " | Home") NoMethodError: You have a nil object when you didn't expect it! You might have expected an instance of Array. The error occurred while evaluating nil.+ # ./spec/controllers/pages_controller_spec.rb:20 Failed examples: rspec ./spec/controllers/pages_controller_spec.rb:17 # PagesController GET 'home' should have the right title </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload