【发布时间】:2014-05-09 17:35:09
【问题描述】:
我目前正在研究 Michael Hartl 的 Ruby on Rails 教程的第 8 章。我遇到了我的测试失败的问题并试图自己解决这个问题,但我意识到它没有用,所以我不得不在这里问。这是我收到的错误消息。
Failures:
1) Authentication signin with invalid information after visiting another page
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:18:in `block (5 levels) in <top (required)>'
2) Authentication signin with valid information
Failure/Error: click_button "Sign in"
NoMethodError:
undefined method `sign_in' for #<SessionsController:0x007f8dbc9d5868>
# ./app/controllers/sessions_controller.rb:8:in `create'
# ./spec/requests/authentication_pages_spec.rb:27:in `block (4 levels) in <top (required)>'
3) Authentication signin with valid information
Failure/Error: click_button "Sign in"
NoMethodError:
undefined method `sign_in' for #<SessionsController:0x007f8dbfdf53a0>
# ./app/controllers/sessions_controller.rb:8:in `create'
# ./spec/requests/authentication_pages_spec.rb:27:in `block (4 levels) in <top (required)>'
4) Authentication signin with valid information
Failure/Error: click_button "Sign in"
NoMethodError:
undefined method `sign_in' for #<SessionsController:0x007f8dbfcf5ea0>
# ./app/controllers/sessions_controller.rb:8:in `create'
# ./spec/requests/authentication_pages_spec.rb:27:in `block (4 levels) in <top (required)>'
5) Authentication signin with valid information
Failure/Error: click_button "Sign in"
NoMethodError:
undefined method `sign_in' for #<SessionsController:0x007f8dbfc0ccc8>
# ./app/controllers/sessions_controller.rb:8:in `create'
# ./spec/requests/authentication_pages_spec.rb:27:in `block (4 levels) in <top (required)>'
6) User pages signup with valid information should create a user
Failure/Error: fill_in "Confirmation", with: "foobar"
Capybara::ElementNotFound:
Unable to find field "Confirmation"
# ./spec/requests/user_pages_spec.rb:24:in `block (4 levels) in <top (required)>'
Finished in 0.60967 seconds
41 examples, 6 failures
Failed examples:
rspec ./spec/requests/authentication_pages_spec.rb:18 # Authentication signin with invalid information after visiting another page
rspec ./spec/requests/authentication_pages_spec.rb:30 # Authentication signin with valid information
rspec ./spec/requests/authentication_pages_spec.rb:32 # Authentication signin with valid information
rspec ./spec/requests/authentication_pages_spec.rb:31 # Authentication signin with valid information
rspec ./spec/requests/authentication_pages_spec.rb:33 # Authentication signin with valid information
rspec ./spec/requests/user_pages_spec.rb:27 # User pages signup with valid information should create a user
Randomized with seed 17229
这是我的身份验证页面规范文件
require 'spec_helper'
describe "Authentication" do
subject { page }
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('Sign out', href: signout_path) }
it { should_not have_link('Sign in', href: signin_path) }
end
end
end
这是我的 session_controller.rb 文件
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
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
def destroy
end
end
我要解决的主要故障,应该是绿色的,是 1。我通过 flash.now 哈希表示我希望闪存在第二次请求后消失,但 rspec 说不。
我不知道问题出在哪里。任何帮助将不胜感激
【问题讨论】:
标签: ruby-on-rails flash rspec controller capybara