Ruby on rails RubyonRails 3教程第8章:单元测试未通过
我正在读哈特尔的书,我读到了第八章。我写了一些我认为应该通过的测试。我已经将我的代码与书中的代码进行了四次检查,并与书中的代码进行了两次检查,但我被难住了。我从RSpec得到以下错误:Ruby on rails RubyonRails 3教程第8章:单元测试未通过,ruby-on-rails,Ruby On Rails,我正在读哈特尔的书,我读到了第八章。我写了一些我认为应该通过的测试。我已经将我的代码与书中的代码进行了四次检查,并与书中的代码进行了两次检查,但我被难住了。我从RSpec得到以下错误: Failures: 1) UsersController POST 'create' should redirect to the user "show" page Failure/Error: response.should redirect_to(user_path(assigns(:user
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
用户\u controller.rb
提前谢谢。如果你们真的想深入研究,或者如果我在帖子中遗漏了什么
我对rails非常陌生,到目前为止我绝对喜欢它。非常感谢您的帮助。请仔细查看您的
用户\u控制器\u规范
“成功”规范:它将在何时创建@attr
?在每次测试之前,还是在“应该创建新用户”测试之前?你在所有的“POST'create'”测试中使用它
一旦你做出了非特定规格的测试,你的测试就会通过
(顺便说一句,将代码放在git中很方便,但前提是您发布的代码实际上已经签入,否则……就没有那么多了;)
它“应该重定向到用户显示页面”
和
它“应该有一个欢迎的信息”
是在
描述“成功”是什么
循环,因为您可能发现您的记录没有保存。尝试一次删除一个验证。哈哈!我真不敢相信我错过了。早上2点的时候,所有这些人都很难跟上@是的,的确。。。所有用户的属性都为零是“赠品”:)是的,我想知道为什么它似乎没有节约。我猜这就是你在凌晨盯着代码看得太厉害的原因。再次感谢!
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
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
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
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