Search code examples
ruby-on-railsrspec-railsruby-on-rails-4

Ruby on Rails Tutorial by Michael Hartl Chapter 7.1.3 - Failing Test


I've spent a couple hours trying to figure out this tiny little piece of the puzzle before moving on in the tutorial so it doesn't blow up on my later. Because of that, I know issues where tests are failing like these are a dime a dozen, so I apologize. In the "7.1.3 Testing the user show page (with factories)" section of the tutorial (link provided below), I cannot get the signup page tests to pass. This seems like it's so trivial, but I can't find anything wrong in any of the seemingly endless files where there could be an issue (the controller, routes, the test itself, the user model, and so on). No one online seems to have had this issue either, so I figure it's got to be some random setting or something on my rails project.

Anyway, here is what I believe is the relevant code:
The error from running the user_pages_spec test in sublime:

Failures:

  1) User pages signup page 
     Failure/Error: it { should have_content('Sign up') }
       expected #has_content?("Sign up") to return true, got false
     # ./spec/requests/user_pages_spec.rb:18:in `block (3 levels) in <top (required)>'

  2) User pages signup page 
     Failure/Error: it { should have_title(full_title('Sign up')) }
       expected #has_title?("Ruby on Rails Tutorial Sample App | Sign up") to return true, got false
     # ./spec/requests/user_pages_spec.rb:19:in `block (3 levels) in <top (required)>'

Finished in 0.16194 seconds
4 examples, 2 failures

Failed examples:

rspec ./spec/requests/user_pages_spec.rb:18 # User pages signup page 
rspec ./spec/requests/user_pages_spec.rb:19 # User pages signup page 

Randomized with seed 46255

[Finished in 2.7s with exit code 1]

spec/requests/user_pages_spec.rb

require 'spec_helper'

describe "User pages" do

  subject { page }

  describe "profile page" do
    let(:user) { FactoryGirl.create(:user) }
    before { visit user_path(user) }

    it { should have_content(user.name) }
    it { should have_title(user.name) }
  end

  describe "signup page" do
    before { visit signup_path }

    it { should have_content('Sign up') }
    it { should have_title(full_title('Sign up')) }
  end
end

app/views/users/new.html.erb

<% provide(:title, 'Sign Up') %>
<h1>Sign Up</h1>
<p>Find me in app/views/users/new.html.erb</p>

app/controller/users_controller.rb

class UsersController < ApplicationController

  def show
    @user = User.find(params[:id])
  end

  def new
  end
end

config/routes.rb

SampleApp::Application.routes.draw do
  resources :users
  root to: 'static_pages#home'
  match '/signup',  to: 'users#new',            via: 'get'
  match '/help',    to: 'static_pages#help',    via: 'get'
  match '/about',   to: 'static_pages#about',   via: 'get'
  match '/contact', to: 'static_pages#contact', via: 'get'
end

Let me know if you can think of any other code you may need to see. I included new.html.erb because I think that the signup test is looking on that page, but I must be wrong on that. If anyone could let me know what I'm doing wrong that is causing this failure, I would be eternally grateful.

If you could convince me that the whole test driven development concept isn't a complete pain in the rear, then you could get some extra brownie points on top of that. I've found this tutorial to be pretty enjoyable, but any time I get to a testing section I lose all motivation to continue, especially when it doesn't work. I know it's better for your code in the long run, but for just getting started I'm beginning to think I should just ignore it at risk of quitting.

http://ruby.railstutorial.org/book/ruby-on-rails-tutorial?version=4.0#code-name_title_and_heading


Solution

  • Did you try to replace 'Sign up' by 'Sign Up' in your spec/requests/user_pages_spec.rb ?