简体   繁体   中英

Trouble in Chapter 8 of Michael Hartl's Rails Tutorial - CSS Expects

I am currently working my way through Michael Hartl's Rails Tutorial , and have been unable to make an rspec test work when he is saying it should. These are the tests right after listing 8.10. This is in the sign-in sign out chapter, and the troublesome authentication tests is as follows:

require 'spec_helper'

describe "Authentication" do

  subject { page }


  describe "signin" do
    before { visit signin_path }

    describe "with invalid information" do
        before { click_button "Sign in" }

        it { should have_selector('title', text: 'Sign in') }
        it { should have_selector('div.alert.alert-error', text: 'Invalid') }

        #describe "after visiting another page" do
        #   before { click_link "Home" }
        #   it { should_not have_selector('div.alert.alert-error') }
        #end
    end

end

I believe the two describe signin tests should be passing, but the "it { should have_selector('div.alert.alert-error', text: 'Invalid') }" test is currently showing:

Failure/Error: it { should have_selector('div.alert.alert-error', text: 'Invalid') }
   expected css "div.alert.alert-error" with text "Invalid" to return something
 # ./spec/requests/authentication_pages_spec.rb:21:in `block (4 levels) in <top (required)>'

My sessions controller is as follows:

  class SessionsController < ApplicationController

  def new
  end

 def create
    user = User.find_by_email(params[:session][:email])
    if user && user.authenticate(params[:session][:password])
       #I haven't gotten to this tutorial part yet
    else
       flash.now[:error] = "Invalid email/password combination"
       render 'new'
    end
  end

  def destroy
  end
end

When I test this with the built in Rails server, the browser source code contains:

    <!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->    
  </head>
  <body>
    <header class="navbar navbar-fixed-top navbar-inverse">
  <div class="navbar-inner">
    <div class="container">
      <a href="/" id="logo">sample app</a>
      <nav>
        <ul class="nav pull-right">
          <li><a href="/">Home</a></li>
          <li><a href="/help">Help</a></li>
          <li><a href="#">Sign in</a></li>
        </ul>
      </nav>
    </div>
  </div>
</header>
    <div class="container">
        <div class="alert alert -error">Invalid email/password combination</div>
      <h1>Sign in</h1>

<div class="row">
  <div class="span6 offset3">
    <form accept-charset="UTF-8" action="/sessions" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="Gh9r4Qf0R00A31u69ETFKeAt57nj6Qqai5iuBISWOWE=" /></div>

      <label for="session_email">Email</label>
      <input id="session_email" name="session[email]" size="30" type="text" />

      <label for="session_password">Password</label>
      <input id="session_password" name="session[password]" size="30" type="password" />

      <input class="btn btn-large btn-primary" name="commit" type="submit" value="Sign in" />
</form>

I think, for my purposes, the critical line here is:

<div class="container">
    <div class="alert alert -error">Invalid email/password combination</div>
  <h1>Sign in</h1>

What I think should be happening is that my test is visiting this page, and should be seeing that an alert alert -error division (with acceptable text) exists in the produced html, and pass the test. I understand that this is not happening, but one question I do have is whether my high level understanding of this process is correct.

I have been looking around stackoverflow for other tutorial questions. While there are some similar questions, I have not been able to use them to answer my question, and so I am asking this question, because I think this qualifies as a new question. I did see that the tutorial questions gave a lot of files, and I am just trying to post the relavent files here. However, here is my current routes.rb file

SampleApp::Application.routes.draw do
  resources :users
  resources :sessions, only: [:new, :create, :destroy]

  root to:   'static_pages#home'

  match '/signup',  to:  'users#new'
  match '/signin',  to: 'sessions#new'
  match '/signout', to: 'sessions#destroy', via: :delete
  match '/help',    to:  'static_pages#help'
  match '/about',   to:  'static_pages#about'
  match '/contact', to:  'static_pages#contact'
end

And here is my current new.html.erb file, which has file path in ~/rails_projects/sample_app/app/views/sessions/new.html.erb

<% provide(:title, "Sign in") %>
<h1>Sign in</h1>

<div class="row">
  <div class="span6 offset3">
    <%= form_for(:session, url: sessions_path) do |f| %>

      <%= f.label :email %>
      <%= f.text_field :email %>

      <%= f.label :password %>
      <%= f.password_field :password %>

      <%= f.submit "Sign in", class: "btn btn-large btn-primary" %>
    <% end %>

    <p>New user? <%= link_to "Sign up now!", signup_path %></p>
  </div>
</div>

I plan on moving on with the tutorial, because it seems like the functionality is unhindered. However, I fear that later my lack of understanding in these tests will seriously mess things up. It would be great if someone has had a similar problem cold help point me towards a solution.

Thanks in advance!

Inspect from your web page source code:

<div class="alert alert -error">Invalid email/password combination</div>

The target div has class alert and -error . However, in your testcase, you are expecting a div with class alert and alert-error

it { should have_selector(' div.alert.alert-error ', text: 'Invalid') }

I think you have typed an extra space between alert and -error , it should be alert-error (without space).

You should check your layout view, because sessions/new.html.erb you pasted doesn't include the source that generates this line of HTML.

UPDATE : As commented by Dylan Markow, you might have typed an extra space in <div class="alert alert-<%= key %>"> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM