我在第8章流動ROR教程。我被測試authentication_pages卡住了。我已經三次查過這本書,沒有發現任何問題。任何人請看看,你的幫助將不勝感激!Rspec測試認證頁面無法通過
Failures:
1) AuthenticationPages signin with valid information
Failure/Error: fill_in "Password", with: user.Password
NoMethodError:
undefined method `Password' for #<User:0x007fee4d55ed60>
# ./spec/requests/authentication_pages_spec.rb:27:in `block (4 levels) in <top (required)>'
2) AuthenticationPages signin with valid information
Failure/Error: fill_in "Password", with: user.Password
NoMethodError:
undefined method `Password' for #<User:0x007fee4d668d50>
# ./spec/requests/authentication_pages_spec.rb:27:in `block (4 levels) in <top (required)>'
3) AuthenticationPages signin with valid information
Failure/Error: fill_in "Password", with: user.Password
NoMethodError:
undefined method `Password' for #<User:0x007fee4ba5ed08>
# ./spec/requests/authentication_pages_spec.rb:27:in `block (4 levels) in <top (required)>'
4) AuthenticationPages signin with valid information
Failure/Error: fill_in "Password", with: user.Password
NoMethodError:
undefined method `Password' for #<User:0x007fee4d68b008>
# ./spec/requests/authentication_pages_spec.rb:27:in `block (4 levels) in <top (required)>'
Finished in 0.72208 seconds
44 examples, 4 failures
Failed examples:
rspec ./spec/requests/authentication_pages_spec.rb:30 # AuthenticationPages signin with valid information
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
rspec ./spec/requests/authentication_pages_spec.rb:33 # AuthenticationPages signin with valid information
authentication_pages_spec.rb文件:
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_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])
else
flash.now[:error]= 'Invalid email/password combination' # Not quite right!
render 'new'
end
end
def destroy
end
end
的routes.rb文件
SampleApp::Application.routes.draw do
resources :users
resources :sessions, only: [:new, :create, :destroy]
root to: 'static_pages#home'
match '/signup', to: 'users#new', via: 'get'
match '/signin', to: 'sessions#new', via: 'get'
match '/signout', to: 'sessions#destroy', via: 'delete'
match '/help', to: 'static_pages#help', via: 'get'
match '/about', to: 'static_pages#about', via: 'get'
match '/contact', to: 'static_pages#contact', via: 'get'
end
'user.password'? – apneadiving 2014-09-10 08:32:12
謝謝!這是小寫hehe – Snailwalker 2014-09-10 08:57:22