添加Bootstrap下拉菜单后无法通过rspec测试

时间:2013-08-15 09:05:19

标签: ruby-on-rails rspec capybara

我正在关注迈克尔·哈特尔的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中描述的相同路径/链接重定向您。

那是怎么回事?

3 个答案:

答案 0 :(得分:0)

我认为,您的规范无法在bootstrap下拉菜单中找到您的链接。我想,你只需先点击下拉列表,它就能找到那些链接。试试这个:

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 

答案 1 :(得分:0)

尝试:

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

或者

describe "with valid information", js: true do
  # before
  click_link "Account"

答案 2 :(得分: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) }