我正在参加RoR教程。当所有的rspec测试都变成绿色时,我就无法让我的工作了。我运行bundle exec rspec spec /和四个测试,一个失败,出现此错误:
故障:
1)访问后,身份验证登录信息无效 另一页
Failure/Error: it { should_not have_selector('div.alert.alert-error') } expected #has_selector?("div.alert.alert-error") to return false, got true # ./spec/requests/authentication_pages_spec.rb:25:in `block (5 levels) in <top (required)>'Finished in 7.2 seconds 62 examples, 1
失败
失败的例子:
rspec ./spec/requests/authentication_pages_spec.rb:25#Authentication 在访问另一页后登录无效信息
我的authentication_pages_spec.rb文件:
require 'spec_helper'
describe "Authentication" 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') }
describe "after visiting another page" do
before { click_link "Home" }
it { should_not have_selector('div.alert.alert-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('Settings', href: edit_user_path(user)) }
it { should have_link('Sign out', href: signout_path) }
it { should_not have_link('Sign in', href: signin_path) }
describe "follower by signout" do
before {click_link "Sign out"}
it {should have_link('Sign in')}
end
end
end
describe "authorization" do
describe "for non-signed-in users" do
let(:user) { FactoryGirl.create(:user) }
describe "in the Users controller" do
describe "visiting the edit page" do
before { visit edit_user_path(user) }
it { should have_title('Sign in') }
end
describe "submitting to the update action" do
before { patch user_path(user) }
specify { expect(response).to redirect_to(signin_path) }
end
end
end
end
end
application.html.erb
<!DOCTYPE html>
<html>
<head>
<title><%= full_title(yield(:title)) %></title>
<%= stylesheet_link_tag "application", media: "all",
"data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>
<%= render 'layouts/shim' %>
</head>
<body>
<%= render 'layouts/header' %>
<div class="container">
<% flash.each do |key, value| %>
<%= content_tag(:div, value, class: "alert alert-#{key}") %>
<% end %>
<%= yield %>
<%= render 'layouts/footer' %>
<%= debug(params) if Rails.env.development? %>
</div>
</body>
</html>
home.html.erb:
<div class="center hero-unit">
<h1>Welcome to the Sample App</h1>
<h2>
This is the home page for the
<a href="http://railstutorial.org/">Ruby on Rails Tutorial</a>
sample application.
</h2>
<%= link_to "Sign up now!", signup_path, class: "btn btn-large btn-primary" %>
</div>
<%= link_to image_tag("rails.png", alt: "Rails"), 'http://rubyonrails.org/' %>
static_pages_controller.rb:
class StaticPagesController < ApplicationController
def home
end
def help
end
def about
end
def contact
end
end
答案 0 :(得分:1)
请确保您的sessions_controller.rb
create
动作定义如下:
def create
user = User.find_by(email: params[:email].downcase)
if user && user.authenticate(params[:password])
# If the user exists and they give a correct password,
# sign the user in and redirect to the user's show page.
sign_in user
redirect_to user
else
# Create an error message and re-render the signin form.
flash.now[:error] = 'Invalid email/password combination'
render 'new'
end
end
注意flash.now
。它与我们在本教程的其他部分中使用的flash
不同。