首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails教程3第9.1章: Rspec测试失败时应该通过

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

Stack Overflow用户
提问于 2013-04-10 13:14:45
回答 1查看 214关注 0票数 0

我是一个rails新手,并且一直在研究Michael的令人敬畏的Rails 3教程,并取得了巨大的成功。到目前为止,我所有的测试都在他们应该做的时候起作用(根据书)。然而,我在第9章开头,并且正在通过rspec测试--也就是说,浏览器中的功能是完美的。

我检查了所有的东西,甚至查看了示例应用程序的github回购,但仍然无法找到如何治愈我的rspec问题。任何帮助都是非常感谢的。

问题中的错误

代码语言:javascript
复制
    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)>'

其他应用程序文件

spec/requests/authentication_pages_spec.rb

代码语言:javascript
复制
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

spec/requests/user_pages_spec.rb

代码语言:javascript
复制
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

app/views/layouts/_header.html.erb

代码语言:javascript
复制
   <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>

app/views/users/edit.html.erb

代码语言:javascript
复制
<% 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>
EN

回答 1

Stack Overflow用户

发布于 2013-04-11 11:55:52

解决了更新!

谢谢各位的评论。最后,我做了几件事情,让考试通过了:

  • 重新启动的轨道
  • 继续跟踪这本书,直到9.2.1年底

做完这两件事之后,测试套件如预期的那样变成了绿色。希望这能帮助那些陷入同样困境的人。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15926926

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档