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