0
我玩轨3.0.6与1.9.2红宝石, 应用正在浏览器,但没有在测试..未能测试创建行动
1)我创建的“轨道一个新的Rails应用程序新的myapp”
2)生成一个支架 “轨道生成支架用户的用户名:字符串hashed_password:串盐:后字符串”
3)中,i改变了users_controller有点
# POST /users
# POST /users.xml
def create
@user = User.new(params[:user])
respond_to do |format|
if @user.save
// change @user to usrs_url
format.html { redirect_to(users_url, :notice => "User #{@user.username} was successfully created.") }
format.xml { render :xml => @user, :status => :created, :location => @user }
else
format.html { render :action => "new" }
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
end
end
end
# PUT /users/1
# PUT /users/1.xml
def update
@user = User.find(params[:id])
respond_to do |format|
if @user.update_attributes(params[:user])
// change @user to usrs_url
format.html { redirect_to(users_url, :notice => "User #{@user.username} was successfully updated.") }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
end
end
end
4),所以我尝试修改测试以及:
setup do
@input_attributes = {
:username => '[email protected]',
:password => 'secret',
:password_confirmation => 'secret'
}
@user = users(:one)
end
test "should create user" do
assert_difference('User.count') do
post :create, :user => @input_attributes
end
assert_redirected_to users_path
end
test "should update user" do
put :update, :id => @user.to_param, :user => @input_attributes
assert_redirected_to users_path
end
但创建和更新测试失败
任何人都可以让我知道了什么我做错了什么?
感谢
Loaded suite C:/Ruby192/lib/ruby/1.9.1/rake/rake_test_loader
Started
F.....F
Finished in 5.628874 seconds.
1) Failure:
test_should_create_user(UsersControllerTest) [test/functional/users_controller_test.rb:26]:
"User.count" didn't change by 1.
<3> expected but was
<2>.
2) Failure:
test_should_update_user(UsersControllerTest) [test/functional/users_controller_test.rb:45]:
Expected block to return true value.
7 tests, 9 assertions, 2 failures, 0 errors, 0 skips
require 'digest/sha2'
class User < ActiveRecord::Base
validates :username, :presence => true, :uniqueness => true
validates_format_of :username, :with => /\A^[^\r\[email protected] ][^\r\[email protected] ][email protected][^\r\[email protected] ]+[.][^\r\[email protected] ]+$\Z/i
#password is a fake field
validates :password, :confirmation => true
validate :password_must_be_present
attr_accessor :password_confirmation
attr_reader :password
def password=(password)
if password.present?
generate_salt
self.hashed_password = self.class.encrypt_password(password, salt)
end
end
class << self
def encrypt_password(password, salt)
Digest::SHA2.hexdigest(password + "shrimpy" + salt)
end
def authenticate(username, password)
if user = find_by_username(username)
if user.hashed_password == encrypt_password(password, user.salt)
user
end
end
end
end
private
def password_must_be_present
errors.add(:password, "Missing password") unless hashed_password.present?
end
def generate_salt
self.salt = self.object_id.to_s + rand.to_s
end
end
你是对的......多么尖锐的眼睛。我的拼写错误....它应该是users_path ...我修正了它...但问题仍然存在..不能够通过测试运行 – jojo 2011-04-20 06:38:37
@shrimpy:看起来你正在混合和匹配'users_path'和' users_url'。我不确定,但我敢打赌,assert_redirected_to'只是比较'url_for'输出的字符串,因为它没有请求/主机名的上下文来从路径中构建完整的URL。给它一个机会......值得修复这种或那种方式。 – coreyward 2011-04-20 06:46:28
它没有为我工作....回到错误消息,它抱怨不能保存记录和更新记录到数据库....但我用sqlite3 ...嗯..不应该是一个问题..... confused ... – jojo 2011-04-20 06:52:43