Michael Hart的Ruby on Rails教程 - 第8章失败的测试

时间:2014-05-09 17:35:09

标签: ruby-on-rails flash rspec controller capybara

我目前正在研究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

这是我的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
      # 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说没有。

我很困惑,问题是什么。任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

RSpec告诉您方法sign_in不存在。如果您查看chapter 8.2.2,您将找到使用此方法的帮助程序。

#app/helpers/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
end
相关问题