1

我正在关注 Michael Hartl 的 Rails 教程,我正在为您的应用添加一个 Bootstrap 下拉顶部导航栏,该导航栏会根据用户是否登录而改变。

这是使导航栏发生变化的代码

<% 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 %>
            </li>
          </ul>
        </li>
      <% else %>
        <li><%= link_to 'Sign in', signin_path %></li>
      <% end %>

现在,我做了以下 rspec 测试

describe "with valid information" do
    let(:user) { FactoryGirl.create(:user) }   

    before do
      visit '/signin'
      fill_in "Email",    with: user.email.upcase
      fill_in "Password", with: user.password
      click_button "Sign in"
    end

    it { should have_title(user.name) }
    it { should have_link('Profile',     href: user_path(user)) }
    it { should have_link('Sign out',    href: signout_path) }
    it { should_not have_link('Sign in', href: signin_path) }
end 

保存并重新加载应用程序后,我一切正常。

但即使应用程序代码本身正在运行,rspec 测试也失败了。

Failures:

  1) Authentication with valid information
     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/authentication_pages_spec.rb:38:in `block (3 levels) in <
top (required)>'

  2) Authentication with valid information
     Failure/Error: it { should have_link('Profile',     href: user_path(user))
}
       expected #has_link?("Profile", {:href=>"/users/1"}) to return true, got f
alse
     # ./spec/requests/authentication_pages_spec.rb:37:in `block (3 levels) in <
top (required)>'

  3) Authentication with valid information
     Failure/Error: it { should have_title(user.name) }
       expected #has_title?("Michael Hartl") to return true, got false
     # ./spec/requests/authentication_pages_spec.rb:36:in `block (3 levels) in <
top (required)>'

  4) Authentication with valid information
     Failure/Error: it { should_not have_link('Sign in', href: signin_path) }
       expected #has_link?("Sign in", {:href=>"/signin"}) to return false, got t
rue
     # ./spec/requests/authentication_pages_spec.rb:39:in `block (3 levels) in <
top (required)>'

它表示链接(“巧合地”嵌套在下拉菜单中的链接)没有返回 true。

rspec 规范中的路径与应用程序代码中使用的路径相同,您可以看到应用程序使用 rspec 中描述的相同路径/链接重定向您。

发生什么了?

4

3 回答 3

0

我认为,您的规范无法在引导下拉菜单中找到您的链接。我认为,您只需先单击该下拉菜单,它就可以找到该链接。试试这个:

describe "with valid information" do
  let(:user) { FactoryGirl.create(:user) }   

    before do
      visit '/signin'
      fill_in "Email",    with: user.email.upcase
      fill_in "Password", with: user.password
      click_button "Sign in"
    end

    click_link "Account"

    it { should have_title(user.name) }
    it { should have_link('Profile',     href: user_path(user)) }
    it { should have_link('Sign out',    href: signout_path) }
    it { should_not have_link('Sign in', href: signin_path) }
end 
于 2013-08-15T09:31:52.973 回答
0

我在这里找到了一个适合我的答案: https ://stackoverflow.com/questions/18833789/sign-out-rspec-test-failing-in-rails-tutorial-section-9-1-1

相关部分:

考虑到我需要在更改个人资料之前有效登录,我将其添加到我的user_pages_spec.rb

context "edit" do
let(:user) { FactoryGirl.create(:user) }
before { sign_in user }  <------------
before { visit edit_user_path(user) }
于 2014-06-08T16:43:56.563 回答
0

尝试:

 it { should have_link('Profile',     href: user_path(user), visible: false) }

或者

describe "with valid information", js: true do
  # before
  click_link "Account"
于 2013-08-15T09:37:59.760 回答