Ruby on rails 你签到了吗?Rails教程(Hartl)第8-9章中的函数不起作用

Ruby on rails 你签到了吗?Rails教程(Hartl)第8-9章中的函数不起作用,ruby-on-rails,railstutorial.org,Ruby On Rails,Railstutorial.org,我有一次rspec测试失败 1) User pages edit with valid information should have link "Sign out", {:href=>"/signout"} Failure/Error: it { should have_link('Sign out', href: signout_path) } expected #has_link?("Sign out", {:href=>"/signout"}) to retu

我有一次rspec测试失败

  1) User pages edit with valid information should have link "Sign out", {:href=>"/signout"}
 Failure/Error: it { should have_link('Sign out', href: signout_path)   }
   expected #has_link?("Sign out", {:href=>"/signout"}) to return true, got false
 # ./spec/requests/user_pages_spec.rb:80:in `block (4 levels) in <top (required)>'
下面的代码是正在测试的标题模板(也遵循HartlRails教程)。我已经运行了一些测试(使用
put
),发现在登录后,
signed\u in?
函数向rspec返回false(在浏览器中测试时返回true)


  • 这似乎是rspec的一个问题,我还没有弄清楚该怎么补救


    提前感谢您的帮助。

    更新:此答案的原始版本侧重于清单9.1,结果证明清单9.1是正确的,并且由于以下原因已恢复到当前状态,用户中没有签名。然而,OP的摘要评论中提到的清单9.9的问题是一个真正的问题,将
    sign_in user
    添加到该清单中的修复仍然存在


    这似乎是中的一个bug,它删除了清单9.9中
    之前
    块中的
    登录(用户)
    行,该行在以前的版本中存在。它已被更正。

    在您的RSpec示例中,您在哪里登录?删除的评论;请参阅答案摘要:
    sign_in user
    直到清单9.13中的第9.2.1节才被添加。我在第9.1节的末尾,根据清单9.9中的代码,测试应该通过,其中不包括
    sign\u in user
    (在
    descripe“edit”
    下的
    before
    块中)实际上,这本书与最初写的一样正确。测试通过,直到添加了before过滤器,并且本书明确指出了问题并进行了修复。答案更新,以反映(希望)与MHartl交换后的最终解决方案。@MHartl谢谢,Michael!事实上,这本书的原著是正确的。测试将一直通过,直到添加了before筛选器,并且本书明确指出了问题并在中进行了修复。@MHartl好的,很抱歉我错过了。我将更新答案,并在确认您已将该书恢复到以前的状态后更新链接问题的答案。请参阅更新的答案,其中反映OP在9.9左右是正确的,MHartl在9.1左右是正确的。
    describe "User pages" do
    
      subject { page }
    
      ...
    
      describe "edit" do
        let(:user) { FactoryGirl.create(:user) }
        before { visit edit_user_path(user) }
    
        ...
    
        describe "with valid information" do 
            let(:new_name)  { "New Name" }
            let(:new_email) { "new@example.com"}
            before do
                fill_in "Name",         with: new_name
                fill_in "Email",        with: new_email
                fill_in "Password",     with: user.password
                fill_in "Confirm Password",     with: user.password
                click_button "Save changes"
            end
    
            it { should have_title(new_name) }
            it { should have_selector('div.alert.alert-success')    }
            it { should have_link('Sign out', href: signout_path)   }
            specify { expect(user.reload.name).to   eq new_name     }
            specify { expect(user.reload.email).to  eq new_email    }
        end
      end
    end
    
            <% 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", 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 %>