我正在学习 RoR 教程。在第 8.1.5 章的最后一部分,当所有的 rspec 测试都应该变成绿色时,我就是无法让我的工作。我运行 bundle exec rspec spec/requests/authentication_pages_spec.rb -e "signin with invalid information" 并且在四个测试中,一个失败,出现以下错误:
1) Authentication signin with invalid information after visiting another page
Failure/Error: it { should_not have_selector('div.alert.alert-error') }
expected css "div.alert.alert-error" not to return anything
# ./spec/requests/authentication_pages_spec.rb:25:in `block (5 levels) in <top (required)>'
app/controllers/sessions_controller.rb 的文本如下:
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by_email(params[:session][:email])
if user && user.authenticate(params[:session][:password])
# Sign the user in and redirect to the user's show page.
else
flash.now[:error] = 'Invalid email/password combination'
render 'new'
end
end
def destroy
end
end
spec/requests/authetication_pages_spec.rb 的文本如下:
require 'spec_helper'
describe "Authentication" do
subject { page }
describe "signin page" do
before { visit signin_path }
it { should have_selector('h1', text: 'Sign in') }
it { should have_selector('title', text: 'Sign in') }
end
describe "signin" do
before { visit signin_path }
describe "with invalid information" do
before { click_button "Sign in" }
it { should have_selector('title', text: '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.alert-error') }
end
end
describe "with valid information" do
let(:user) { FactoryGirl.create(:user) }
before do
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
end
it { should have_selector('title', text: 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
如果您有任何想法或想法,请告诉我,我完全被难住了!