更多Ruby on Rails测试问题

时间:2015-10-17 01:53:36

标签: ruby-on-rails ruby ruby-on-rails-3

我在第9章,似乎测试再次失败。以下是相关代码:

sessions_helper.rb:

    module SessionsHelper

  #logs in the given user
  def log_in(user)
    session[:user_id] = user.id
  end

  #Remember a user in a persistent session
  def remember(user)
    user.remember
    cookies.permanent.signed[:user_id] = user.id
    cookies.permanent[:remember_token] = user.remember_token
  end

  def current_user?
    user == current_user
  end

  def current_user
    if (user_id = session[:user_id])
      @current_user ||= User.find_by(id: user_id)
    elsif (user_id = cookies.signed[:user_id])
      user = User.find_by(id: user_id)
      if user && user.authenticated?(cookies[:remember_token])
        log_in user
        @current_user = user
      end
    end
  end

  def logged_in?
    !current_user.nil?
  end

  #Forgets a persistent session
  def forget(user)
    user.forget
    cookies.delete(:user_id)
    cookies.delete(:remember_token)
  end

  def log_out
    forget(current_user)
    session.delete(:user_id)
    @current_user = nil
  end
end

这是users_edit_test.rb:

require 'test_helper'

class UsersEditTest < ActionDispatch::IntegrationTest
  # test "the truth" do
  #   assert true
  # end

  def setup
    @user = users(:michael)
  end

  test "successful edit" do
    log_in_as(@user)
    get edit_user_path(@user)
    assert_template 'users/edit'
    name = "Foo Bar"
    email = "foo@bar.com"
    patch user_path(@user), user: { name: name,
                                    email: email,
                                    password: "",
                                    password_confirmation: ""}
    assert_not flash.empty?
    assert_redirected_to @user
    @user.reload
    assert_equal name, @user.name
    assert_equal email, @user.email
  end

  test "unsuccessful edit" do
    log_in_as(@user)
    get edit_user_path(@user)
    assert_template 'users/edit'
    patch user_path(@user), user: { name: "",
                                    email: "foo@invalid",
                                    password: "foo",
                                    password_confirmation: "bar"}
    assert_template 'users/edit'
  end
end

这是users_controller.rb:

class UsersController < ApplicationController
  before_action :logged_in_user, only: [:edit, :update]
  before_action :correct_user, only: [:edit, :update]
  def show
    @user = User.find(params[:id])
    # debugger
  end

  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)
    if @user.save
      #Handle a successful save
      log_in @user
      flash[:success] = "Welcome to the Sample App!"
      redirect_to @user
    else
      render 'new'
    end
  end

  def edit
    @user = User.find(params[:id])
  end

  def update
    @user = User.find(params[:id])
    if @user.update_attributes(user_params)
      #Handle a successful update.
      flash[:success] = "Profile updated"
      redirect_to @user
    else
      render 'edit'
    end
  end

  private

    def user_params
      params.require(:user).permit(:name, :email, :password, :password_confirmation)
    end

    #Before filters

    # Confirms a logged-in user
    def logged_in_user
      flash[:danger] = "Please login to access this page."
      redirect_to login_url
    end

    def correct_user
      @user - User.find(params[:id])
      redirect_to(root_url) unless @user == current_user
    end
end

最后这里是错误:

1) Failure:
UsersEditTest#test_unsuccessful_edit [/home/robert/sample_app/test/integration/users_edit_test.rb:32]:
expecting <"users/edit"> but rendering with <[]>


  2) Failure:
UsersEditTest#test_successful_edit [/home/robert/sample_app/test/integration/users_edit_test.rb:15]:
expecting <"users/edit"> but rendering with <[]>


  3) Failure:
UsersControllerTest#test_should_redirect_update_when_logged_in_as_wrong_user [/home/robert/sample_app/test/controllers/users_controller_test.rb:37]:
Failed assertion, no message given.


  4) Failure:
UsersControllerTest#test_should_redirect_edit_when_logged_in_as_wrong_user [/home/robert/sample_app/test/controllers/users_controller_test.rb:30]:
Failed assertion, no message given.

这很烦人,因为我没有看到与我所拥有的书和代码有任何不同(大多数部分都是从书中复制和粘贴的)。在书中它显然有效,但是当我进行测试时,我得到了这四个失败。

1 个答案:

答案 0 :(得分:0)

我知道,你的代码中有一个拼写错误。

ES6 Generator方法中,您有:users_controller.rb,其中应为:correct_user

将其更改为:

@user - User.find(params[:id])

看看是否能解决您的问题。

如果没有,请尝试将these two methods添加到@user = User.find(params[:id])模块。

如果这也无法解决您的问题。我建议您克隆original working repo from github并尝试弄清楚您的版本中遗漏了什么。我克隆了这个github仓库,所有的测试都在通过。