Rails教程3第9.1章:Rspec测试在应该通过时失败

时间:2013-04-10 13:14:45

标签: ruby-on-rails-3 rspec railstutorial.org

我是一名铁杆菜鸟,并且已经通过Michael Hartl的精彩Rails 3教程取得了巨大的成功。到目前为止,我所有的测试都应该有效(按照本书)。但是,我正处于beginning of chapter 9并且正在通过失败的rspec测试 - 即浏览器中的功能是完美的。

我已经检查了所有内容,甚至查看了示例应用程序的github回购,但仍然无法弄清楚如何治愈我的rspec问题。非常感谢任何帮助

问题中的错误

    1) Authentication signin with valid information 
       Failure/Error: it { should have_selector('a', 'Sign out', href: signout_path) }
         expected css "Sign out" to return something
       # ./spec/requests/authentication_pages_spec.rb:34:in `block (4 levels) in <top (required)>'

    2) Authentication signin with valid information 
       Failure/Error: it { should have_selector('title', text: user.name) }
         expected css "title" with text "Example User" to return something
       # ./spec/requests/authentication_pages_spec.rb:33:in `block (4 levels) in <top (required)>'

    3) User pages signup edit page 
       Failure/Error: it { should have_selector('title', text: "Edit User" ) }
         expected css "title" with text "Edit User" to return something
       # ./spec/requests/user_pages_spec.rb:72:in `block (5 levels) in <top (required)>'


其他应用程序文件

规格/请求/ authentication_pages_spec.rb

require 'spec_helper'

describe "Authentication" do

  subject { page }

  describe "signin page" do
    before { visit signin_path }

    it { should have_selector('h1',    text: 'Sign in') }
    it { should have_selector('title', text: 'Sign in') }
  end

  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') }
    end

    describe "with valid information" do
      let(:user) { FactoryGirl.create(:user) }
      before do
        fill_in "Email",    with: user.email.upcase
        fill_in "Password", with: user.password
        click_button "Sign in"
      end 

      it { should have_selector('title', text: user.name) }
      it { should have_selector('a', 'Sign out', href: signout_path) }
    end
  end
end


规格/请求/ user_pages_spec.rb

require 'spec_helper'

describe "User pages" do

  subject { page }

  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 "profile page" do
  let (:user) { FactoryGirl.create(:user) } #Using factory girl to create the test 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" 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') }
      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') }
        it { should have_link('Sign out') }
      end
    end

    describe "edit" do
      let(:user) { FactoryGirl.create(:user) }
      before { visit edit_user_path(user) }

      describe "page" do 
        it { should have_selector('h1', text: "Update your profile") }
        it { should have_selector('title', text: "Edit User" ) }
        it { should have_link('change', href: 'http://gravatar.com/emails') }
      end

      describe "with invalid information" do
        before { click_button "Save changes" }

        it { should have_content('error') }
      end
      end
  end
end



应用/视图/布局/ _header.html.erb

   <header class="navbar navbar-fixed-top navbar-inverse">
      <div class="navbar-inner">
         <div class="container">
          <%= link_to "sample app", root_path, id: "logo" %>
          <nav>
            <ul class="nav pull-right">
              <li><%= link_to "Home",    root_path %></li>
              <li><%= link_to "Help",    help_path %></li>
              <% if signed_in? %>
                <li><%= link_to "Users", '#' %></li>
                <li id="fat-menu" class="dropdown">
                  <a href="#" class="dropdown-toggle" data-toggle="dropdown"> Account <b class="caret"></b>
                  </a>
                  <ul class="dropdown-menu">
                    <li><%= link_to "Profile", current_user %></li>
                    <li><%= link_to "Settings", '#' %></li>
                    <li class="divider"></li>
                    <li>
                      <%= link_to "Sign out", signout_path, method: "delete" %>
                    </li>
                  </ul>
                </li>
              <% else %>
              <li><%= link_to "Sign in", signin_path %></li>
              <% end %>
            </ul>
          </nav>
        </div>
      </div>
    </header>



应用/视图/用户/ edit.html.erb

<% provide(:title, "Edit user") %> 
<h1>Update your profile</h1>

<div class="row">
  <div class="span6 offset3">
    <%= form_for(@user) do |f| %>
      <%= render 'shared/error_messages' %>

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

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

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

      <%= f.label :password_confirmation, "Confirm Password" %>
      <%= f.password_field :password_confirmation %>

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

    <%= gravatar_for @user %>
    <a href="http://gravatar.com/emails">change</a>
  </div>
</div>

1 个答案:

答案 0 :(得分:0)

解决了更新!

感谢评论家伙。我最后通过做一些事情来完成测试:

  • 重新启动Rails
  • 继续阅读本书直到9.2.1结尾

完成这两件事后,测试套件按预期变为绿色。希望这可以帮助陷入同样情况的人。