Ruby On Rails 3教程第8章:单元测试没有通过

时间:2011-10-08 08:35:25

标签: ruby-on-rails

我正在研究Hartl的书,我要完成第8章。我已经写了一些我认为应该通过的测试。我根据书中的内容对我的代码进行了四倍检查,然后对照本书github repo中的内容进行了双重检查,但我很难过。我从RSpec那里得到以下错误:

Failures:

  1) UsersController POST 'create' should redirect to the user "show" page
     Failure/Error: response.should redirect_to(user_path(assigns(:user)))
     ActionController::RoutingError:
       No route matches {:action=>"show", :controller=>"users", :id=>#<User id: nil, name: nil, email: nil, created_at: nil, updated_at: nil, encrypted_password: nil, salt: nil>}
     # ./spec/controllers/users_controller_spec.rb:93:in `block (3 levels) in <top (required)>'

  2) UsersController POST 'create' should have a welcome message
     Failure/Error: flash[:success].should =~ /welcome to the sample app/i
       expected: /welcome to the sample app/i
            got: nil (using =~)
     # ./spec/controllers/users_controller_spec.rb:98:in `block (3 levels) in <top (required)>'

Finished in 0.83875 seconds
46 examples, 2 failures

就像我说的那样,我一次又一次地检查了代码。重新启动spork,重新启动rails服务器,没有spork运行。我已经根据书中的代码和github repo检查了它。我甚至在github repo中复制/粘贴了规范和控制器代码,但都无济于事。

我很难过。现在已经很晚了,我需要崩溃。 :)

希望你们其中一个人能看到我不喜欢的东西。这是我到目前为止所得到的......

users_controller_spec.rb

require 'spec_helper'

describe UsersController do
  render_views

  # ...

  describe "POST 'create'" do

    # ...

    describe 'success' do
      before(:each) do
        @attr = { :name => 'New User', :email => 'some-email@gmail.com', :password => 'foobar', :password_confirmation => 'foobar' }
      end

      it 'should create a new user' do
        lambda do
          post :create, :user => @attr
        end.should change(User, :count).by(1)
      end
    end

    it 'should redirect to the user "show" page' do
      post :create, :user => @attr
      response.should redirect_to(user_path(assigns(:user)))
    end

    it 'should have a welcome message' do
      post :create, :user => @attr
      flash[:success].should =~ /welcome to the sample app/i
    end

  end
end

users_controller.rb

class UsersController < ApplicationController
  def new
    @user = User.new
    @title = 'Sign up'
  end

  def show
    @user = User.find params[:id]
    @title = @user.name
  end

  def create
    @user = User.new(params[:user])
    if @user.save
      flash[:success] = 'Welcome to the Sample App!'
      redirect_to @user
    else
      @title = 'Sign up'
      render 'new'
    end
  end
end

user.rb

class User < ActiveRecord::Base
  # Virtual properties (don't exist in db)
  attr_accessor :password

  # Accessible properties
  attr_accessible :name, :email, :password, :password_confirmation
  email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

  validates :name,  :presence => true,
                    :length => { :maximum => 50 }

  validates :email, :presence => true,
                    :format => { :with => email_regex },
                    :uniqueness => { :case_sensitive => false }

  validates :password,  :presence => true,
                        :confirmation => true,
                        :length => { :within => 6..40 }

  before_save :encrypt_password

  # Return true if the user's password matches the submitted password
  def has_password?(submitted_password)
    # Compare encrypted_password with the encrypted version of submitted_password
    encrypted_password == encrypt(submitted_password)
  end

  # Static/Class methods
  def self.authenticate(email, submitted_password)
    user = find_by_email email
    return nil if user.nil?
    return user if user.has_password? submitted_password
  end

  # Private functionality.
  # Anything after the 'private' pragma will be inaccessable from outside the class
  private

    def encrypt_password
      self.salt = make_salt if new_record? # Using ActiveRecord goodness to make sure this only gets created once.
      self.encrypted_password = encrypt(password)
    end

    def encrypt(string)
      secure_hash("#{salt}--#{string}")
    end

    def make_salt
      secure_hash("#{Time.now.utc}--#{password}")
    end

    def secure_hash(string)
      Digest::SHA2.hexdigest(string)
    end
end

的routes.rb

SampleApp::Application.routes.draw do
  #get '/users/new'
  resources :users

  match '/signup' => 'users#new'

  match '/about' => 'pages#about'
  match '/contact' => 'pages#contact'
  match '/help' => 'pages#help'

  root :to => 'pages#home'
end

提前致谢。如果你们真的想要深入挖掘,或者我在帖子中错过了某些内容,here's my code

我对rails非常陌生,到目前为止一直非常喜欢它。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

仔细查看您的users_controller_spec“成功”规范:何时会创建@attr?在每次测试之前,还是在“应该创建新用户”测试之前?您在所有“POST'create'”测试中使用 ...

一旦你做出非特定规格的测试,你的测试就会通过。

(顺便说一句,在git中使用代码很方便,但只有当您发布的代码实际签入时,否则......不会那么多;)

答案 1 :(得分:1)

您       它“应该重定向到用户显示页面” 和       它“应该有一个欢迎信息”

在...之外     描述“成功” 环

相关问题