测试 在进行 rspec 测试时 出现了问题

snailseason · 2014年09月13日 · 最后由 snailseason 回复于 2014年09月14日 · 2863 次阅读

hello 大家,我正在阅读 rails 4 的教程,在进行到第八章测试 authentication_pages 时,总是通不过,查了很多资料都不行,我也在 stackover 发了一篇帖子,帖子地址:http://stackoverflow.com/questions/25824363/testing-with-rspec-couldnt-get-over-it-any-one 一晚上了没人回。呵呵,所以回到咱们中国社区这来看看,请高手帮我看看。以下时细节:

Snailwalkers-MacBook-Pro:sample_app snailwalker$ bundle exec rspec spec/
.................................FF.........

Failures:

  1) AuthenticationPages signin with valid information 
     Failure/Error: it { should have_link('Profile', href: user_path(user)) }
       expected #has_link?("Profile", {:href=>"/users/82"}) to return true, got false
     # ./spec/requests/authentication_pages_spec.rb:31:in `block (4 levels) in <top (required)>'

  2) AuthenticationPages signin 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:32:in `block (4 levels) in <top (required)>'

Finished in 0.92605 seconds
44 examples, 2 failures

Failed examples:

rspec ./spec/requests/authentication_pages_spec.rb:31 # AuthenticationPages signin with valid information 
rspec ./spec/requests/authentication_pages_spec.rb:32 # AuthenticationPages signin with valid information 

Randomized with seed 61227

app/views/users/new.html.erb

<% provide(:title, 'Sign up') %>
<h1>Sign up</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, "Confirmation" %>
      <%= f.password_field :password_confirmation %>

      <%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
    <% end %>
  </div>
</div>

app/views/users/show.html.erb

<%= @user.name %>, <%= @user.email %>
<% provide(:title, @user.name) %>
<div class="row">
  <aside class="span4">
    <section>
<h1>
    <%= @user.name %>
    <%= gravatar_for @user %>
  </h1>
    </section>
  </aside>
</div>

app/views/sessions/new.html.erb

<% provide(:title, "Sign in") %>
<h1>Sign in</h1>
<div class="row">
    <div class="span6 offset3">
        <%= form_for(:session, url: sessions_path) do |f| %>
        <%= f.label :email %>
        <%= f.text_field :email %>

        <%= f.label :password %>
        <%= f.password_field :password %>
        <%= f.submit "Sign in", class: "btn btn-large btn-primary" %>
        <% end %>
        <p>New user? <%= link_to "Sign up now!", signup_path %></p>
    </div>
</div>

spec/requests/authentication_pages_spec.rb file :

require 'spec_helper'

describe "AuthenticationPages" do
    subject { page }
    describe "signin page" do
    before { visit signin_path }
it { should have_content('Sign in') }
it { should have_title('Sign in') }
end
describe "signin" do
    before { visit signin_path }
    describe "with invalid information" do
        before { click_button "Sign in"}

        it { should have_title('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.alter-error') }
        end
    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_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
end
end

sessions_controllers.rb file.

class SessionsController < ApplicationController

  def new
  end

  def create
    user = User.find_by(email: params[:session][:email].downcase)
    if user && user.authenticate(params[:session][:password])
      redirect_to user
    else
      flash.now[:error] = 'Invalid email/password combination'
      render 'new'
    end
  end

  def destroy
  end
end

LZ, 最好附上 github 代码链接

@snailseason

测试都通过了。。。

➜  Sample_app git:(master) ✗ bundle exec rspec
No DRb server is running. Running in local process instead ...
.................................

Finished in 0.45558 seconds
33 examples, 0 failures

Randomized with seed 38252

hi 谢谢你的尝试,我刚发现我最新的几个文件没有 commit 上去,所以你测试才是通过的,稍等等我找到方法把这些 commit 上去吧,尝试了几下不行 呵呵

已经 commit 了,谢谢你啦,麻烦你在帮忙看看

问题在于你的代码还没写完,_header.html.erb 中 还没有定义 "Profile" 和 "Sign out", 所以登录成功以后找不到上面两个连接。

<header class="navbar navbar-fixed-top navbar-inverse">
  <div class="navbar-inner">
    <div class="container">
      <%= link_to "Snail Travel Season", root_path, id: "logo" %>
      <nav>
        <ul class="nav pull-right">
          <li><%= link_to "Home",    root_path %></li>
          <li><%= link_to "help",    help_path %></li>
          <li><%= link_to "Sign in", signin_path %></li>
        </ul>
      </nav>
    </div>
  </div>
</header>

继续往后面看,写到代码 8.24,所有测测试就可以通过了。

代码 8.24:根据登录状态改变导航链接 app/views/layouts/_header.html.erb”

哈哈 谢谢,明白了,但是我看作者说的测试可以通过了,结果我一直通不过。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号