Ruby on rails Rspec测试失败Hartl教程第9.1章-编辑用户标题失败

Ruby on rails Rspec测试失败Hartl教程第9.1章-编辑用户标题失败,ruby-on-rails,rspec,railstutorial.org,Ruby On Rails,Rspec,Railstutorial.org,我仍然停留在编辑用户第9.1.1章的测试中,尽管我已经解决了第一组错误消息——感谢你们在这方面的帮助。 一个新的测试失败错误出现在我所在的位置,我被鼓励提出一个新问题。我会把我的更新代码放在这里 我正在运行这个测试:$bundle exec rspec spec/requests/user\u pages\u spec.rb-e“edit page” 我只是不知道错误是从哪里来的,经过大量的搜索。 我还更新了水豚2.0.0 Failures: 1) User pages signup ed

我仍然停留在编辑用户第9.1.1章的测试中,尽管我已经解决了第一组错误消息——感谢你们在这方面的帮助。 一个新的测试失败错误出现在我所在的位置,我被鼓励提出一个新问题。我会把我的更新代码放在这里

我正在运行这个测试:$bundle exec rspec spec/requests/user\u pages\u spec.rb-e“edit page” 我只是不知道错误是从哪里来的,经过大量的搜索。 我还更新了水豚2.0.0

Failures:

  1) User pages signup edit page 
     Failure/Error: it { should have_selector('title', text: "Edit user") }
     Capybara::ExpectationNotMet:
       expected to find css "title" with text "Edit user" but there were no matches. Also found "",         which matched the selector but not all filters.
     # ./spec/requests/user_pages_spec.rb:60:in `block (5 levels) in <top (required)>'

Finished in 0.56475 seconds
3 examples, 1 failure
这是我修改过的用户控制器:

class UsersController < ApplicationController

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

  def new
    @user = User.new
  end

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

  def edit
    @user = User.find(params[:id])
  end
end
以及头文件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", users_path %></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", edit_user_path(current_user) %></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>


看起来您的描述块嵌套可能有误

'用户页面注册编辑页面'

这表明编辑页面嵌套在“注册”中,它应该位于用户页面下

这可能会导致上下文错误

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_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 }

    describe "with invalid information" do
      it "should not create a user" do
        expect { click_button submit }.not_to change(User, :count)
      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 }
          it { should have_link('Sign out') }
        end
      end
    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

为标题{'Edit user'}尝试
content\u
这一行没有改变Error header标记也没有关闭,尽管它可能是一个片段。header标记是原始的。谢谢Michael。谢谢@Muttollamb我会研究一下。你建议从哪里学习Rails惯例中的嵌套?这不是关于Rails中的嵌套,而是关于如何在RSpec中设置测试上下文。vario美国级别的descripe块设置了不同的上下文。我已经更新了答案以提供帮助-基本上我认为您的do,end是无序的为了在用户页面下嵌套编辑页面,我是否需要更改缩进或移动descripe部分(最后16行左右)向上?区块是否需要以不同的顺序放置?我正在尝试研究hartl在github上的代码。如果您复制代码,并与您的初始帖子进行比较,在开始描述“编辑”之前,有一个和结束描述“注册”,这是唯一的更改。不幸的是,我添加了额外的“结束”,但它没有更改e错误:(我当然清理了代码-谢谢@muttonlab的提示
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: full_title('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') }

     describe "after visiting another page" do
      before { click_link "Home" }

    it { should_not have_selector('div.alert.alert-error') }
  end
end


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

    it { should have_selector('title',   text: user.name) }
    it { should have_link('Profile', href: user_path(user)) }
        #it { should have_link('Settings', href: edit_user_path(user)) }
    it { should have_link('Sign out', href: signout_path) }
    it { should_not have_link('Sign in', href: signin_path)}


  describe "followed by signout" do
    before { click_link "Sign out" }
    it { should have_link('Sign in') }  
end
end 
end
end
<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", users_path %></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", edit_user_path(current_user) %></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>
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_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 }

    describe "with invalid information" do
      it "should not create a user" do
        expect { click_button submit }.not_to change(User, :count)
      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 }
          it { should have_link('Sign out') }
        end
      end
    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