Michael Hartl的Ruby on Rails教程未通过测试第8章

时间:2012-07-28 16:18:59

标签: ruby-on-rails ruby rspec

我是Rails的新手。我正在遵循Michael Hartl的教程,Ruby on Rails教程 - 使用Rails学习Web开发(http://ruby.railstutorial.org/)。当我尝试通过一些测试时,我发现了一个错误。我执行 bundle exec rspec 时的输出是下一个:

.........................................F.....

Failures:

  1) Authentication signin with invalid information 
     Failure/Error: it { should have_selector('div.alert.alert-error', text: 'Invalid') }
       expected css "div.alert.alert-error" with text "Invalid" to return something
     # ./spec/requests/authentication_pages_spec.rb:23:in `block (4 levels) in <top (required)>'

Finished in 1.39 seconds
47 examples, 1 failure

Failed examples:

rspec ./spec/requests/authentication_pages_spec.rb:23 # Authentication signin with invalid information

接下来的文件变化很大,我认为其中一个文件可能导致错误:

authentication_pages_rspec.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

sessions_controler.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_in user
      redirect_to user
    else
      flash.now[:error] = 'Invalid email/password combination'
      render 'new'
    end
  end

  def destroy
  end
end

sessions_helper.rb

module SessionsHelper

  def sign_in(user)
    cookies.permanent[:remember_token] = user.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
    @current_user ||= User.find_by_remember_token(cookies[:remember_token])
  end
end

如果您需要任何其他文件,请告诉我,我会发布。提前谢谢。

1 个答案:

答案 0 :(得分:2)

我浏览了你的代码,除了

中的分号
 self.current_user = user;

我没有看到任何奇怪的事情。您还没有发布您正在测试的方法,

if user && user.authenticate(params[:session][:password])
但是,问题可能就在那里。

查找问题的一般步骤:

  • 如果您正在使用它,请重启spark。
  • 手动测试失败的规范:功能是否有效?
  • 如果没有,请修理
  • 如果功能应该有效,但规范仍然失败,请在测试期间使用launchy和save_and_open_page查看页面。

增加1:

嗯,你的规范中的那部分

before { visit signin_path }

describe "with invalid information" do
  before { click_button "Sign in" }

引导您进入SessionController创建操作(因为链接链接到在config / routes.rb中映射的signin_path),可能出错的主要逻辑是user.authenticate(params[:session][:password]) - 由has_secure_password提供。 / p>