2014-09-10 92 views
0

我在第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 
+1

'user.password'? – apneadiving 2014-09-10 08:32:12

+0

谢谢!这是小写hehe – Snailwalker 2014-09-10 08:57:22

回答

1

我觉得apneadiving是对的,它看起来应该是小写字母。我也看看GitHub code for the tutorial

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 

它也有小写。根据David Flanagan,Yukihiro Matsumoto的Ruby编程语言书,只要确定我也检查了Ruby是一种区分大小写的语言。

+0

@Snailwalker它可能是你缺少sample_app/app/views文件夹(可能是sample_app/app/views/sessions/new.html.erb模板),或者你可能需要添加一个渲染方法调用到你的控制器。对不起,我没有你的代码,但我希望这个链接将有助于进一步:http://stackoverflow.com/questions/3019369/rails-message-actionviewmissingtemplate – jyrkim 2014-09-10 09:35:12