Rails教程8.3认证测试未通过

时间:2014-10-10 22:13:56

标签: ruby-on-rails ruby-on-rails-3 authentication rspec tdd

我对RoR相对较新,并且经历了Hart的教程并遇到了一个小问题。我希望你能帮忙!

我完成了第8章,并试图让最后一个测试通过,但我一直失败。我是所有这一切的新手,所以任何帮助将不胜感激。我查看了其他类似的问题,但没有真正找到我的解决方案。而且...我知道这只是一个测试,但我对这些事情超级肛门,我不会继续前进,直到我100%知道我做错了什么。断开连接在哪里?我在这里看不到什么?

失败测试

  

没有DRb服务器正在运行。在本地进程中运行而不是......   .........................................˚F......

     

故障:

     

1)认证登录,然后注销        失败/错误:在{click_link“注销”}之前        水豚:: ElementNotFound:          无法找到“退出”链接        './spec/requests/authentication_pages_spec.rb:34:in'块(4级)'

     

完成0.67649秒48例,1次失败

     

失败的例子:

     

rspec ./spec/requests/authentication_pages_spec.rb:35#身份验证   登录然后注销

     

随机种子40273


Authentication_pages_spec.rb文件

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 { sign_in user }

      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) }
    end

    describe "followed by signout" do
      before { click_link "Sign out" }
      it { should have_link('Sign in') }
    end
  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])
      sign_in user
      redirect_to user
    else
      flash.now[:error] = 'Invalid email/password combination' 
      render 'new'
    end
  end

  def destroy
    sign_out
    redirect_to root_url
  end
end

sessions_helper.rb代码

module SessionsHelper

  def sign_in(user)
    remember_token = User.new_remember_token
    cookies.permanent[:remember_token] = remember_token
    user.update_attribute(:remember_token, User.digest(remember_token))
    self.current_user = user
  end

  def signed_in?
    !current_user.nil?      
  end
  def current_user=(user)
    @current_user = user
  end

  def current_user
    remember_token = User.digest(cookies[:remember_token])
    @current_user ||= User.find_by(remember_token: remember_token)
  end

  def sign_out
    current_user.update_attribute(:remember_token, User.digest(User.new_remember_token))
    cookies.delete(:remember_token)
    self.current_user = nil
  end
end

1 个答案:

答案 0 :(得分:0)

您只能在一次测试FactoryGirl中使用"with valid information"创建新用户。当您进行下一次测试时,您没有此用户,因此您需要将此测试放入do end"with valid information"

describe "Authentication" do

  subject { page }

  describe "signin" do
    # You run it before all tests in this block
    before { visit signin_path }

    describe "with invalid information" do
      # So, here you visit signin_path and click "Sign in" link
      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

    # Before this test you only visit signin_path
    describe "with valid information" do
      # After this you create new user with FactoryGirl...
      let(:user) { FactoryGirl.create(:user) }
      # ...and sign him in (before every test in this block)
      before { sign_in user }

      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) }
    end

    # Here you again visit signin_path, but you don't have FactoryGirl user.
    # You can have this user, if you move this block to previous: "with valid information"
    describe "followed by signout" do
      before { click_link "Sign out" }
      it { should have_link('Sign in') }
    end
  end
end

所以,像这样重构你的代码:

describe "with valid information" do
  let(:user) { FactoryGirl.create(:user) }
  before { sign_in user }

  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 "followed by signout" do
    before { click_link "Sign out" }
    it { should have_link('Sign in') }
  end
end

这应该有所帮助。

完成RailsTutorial之后,我建议你阅读"Everyday Rails: Testing with RSpec"。测试中的should模式已经过时了,最好使用expect -> to模式。