Rails教程第7章失败的Rspec

时间:2013-04-04 20:01:40

标签: railstutorial.org

我正在阅读ROR教程并在问题4上遇到障碍。

Failures:
  1) UserPages signup with valid information after saving the user      
Failure/Error: it    { should have_selector('div.alert.alert-success', text: 'Welcome') }
       expected css "div.alert.alert-success" with text "Welcome" to return something
     # ./spec/requests/user_pages_spec.rb:54:in `block (5 levels) in <top (required)>'

Finished in 3.26 seconds
39 examples, 1 failure
Failed examples:

rspec ./spec/requests/user_pages_spec.rb:54 # UserPages signup with valid information    after saving the user

user_controllers.rb

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

  def create
    @user = User.new(params[:user])
    if @user.save
      # Handle a successful save.
      flash[:success] = "Welcome to the Sample App!"
      redirect_to @user
    else 
      render 'new'
    end
  end

  def new
    @user = User.new
 end
end

user_pages_spec.rb

require 'spec_helper'

describe "UserPages" do
  subject { page }

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

    it { should have_selector('h1', text: user.name) }
    it { should have_selector('title', text: user.name) }
  end

  describe "signup page" do
    before { visit signup_path }
    it {should have_selector('h1', text: 'Sign up') }
    it {should have_selector('title', text: full_title('Sign up')) }
  end

  describe "signup" do
    before { visit signup_path }
    let(:submit) { "Create my account" }


    describe "with invalid information" do
      it "should not create a user" do
        expect { click_button submit }.not_to change(User, :count)
      end

      describe "after submission" do
        before { click_button submit }
        it { should have_selector('title', text: 'Sign up') }
        it { should have_content('error') }
        it { should have_content('The form contains')}
      end
    end

    describe "with valid information" do
      before do
        fill_in "Name",         with: "Example User"
        fill_in "Email",        with: "user@example.com"
        fill_in "Password",     with: "foobar"
        fill_in "Confirmation", with: "foobar"
      end

      it "should create a user" do
        expect { click_button submit }.to change(User, :count).by(1)
      end

      describe "after saving the user" do
        before { click_button submit }
        let(:user) { User.find_by_email('user@example.com') }
        it { should have_selector('title', text: user.name) }
        it { should have_selector('div.alert.alert-success', text: 'Welcome') }
      end

    end
  end
end

application.html.erb

<!DOCTYPE html>
<html>
  <head>
    <title><%= full_title(yield(:title)) %></title>
    <%= stylesheet_link_tag    "application", media: "all" %>
    <%= javascript_include_tag "application" %>
    <%= csrf_meta_tags %>
    <%= render 'layouts/shim' %>
  </head>
  <body>
    <%= render 'layouts/header' %>
    <div class="container">

      <% flash.each do |key, value| %>
        <div class="alert alert-"<%= key %>"><%= value %></div>
      <% end %>

      <%= yield %>
      <%= render 'layouts/footer' %>
      <%= debug(params) if Rails.env.development? %>
    </div>
  </body>
</html>

我试过手动硬编码

<div class="alert alert-success">Welcome</div>

这导致测试通过,所以我排除了user_pages_spec文件。不知道错误在哪里,因为它在注册时打印成功消息。

1 个答案:

答案 0 :(得分:1)

看起来你有额外的报价

<div class="alert alert-"<%= key %>"><%= value %></div>

应该是

<div class="alert alert-<%= key %>"><%= value %></div>