如何解决未能测试创建动作
| 我正在使用Rails 3.0.6和ruby 1.9.2, 该应用程序可以在浏览器上运行,但无法测试。 1)我通过“ rails new myapp \”创建了一个新的Rails应用程序 2)生成支架\“ rails生成支架用户名:string hashed_password:string salt:string \” 3)之后,我更改了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 => \'username@goodmail.com\',: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\\n@ ][^\\r\\n@ ]+@[^\\r\\n@ ]+[.][^\\r\\n@. ]+$\\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
解决方法
assert_redirected_to user_path
是单数。您甚至可能没有定义单一的user
资源路由。您想要的可能是带有多个用户的assert_redirected_to users_path
。版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。