0

Rails 新手,非常感谢您的时间。所以我正在运行以下测试:

   test "does not validate without" do
@user.update_attributes(email: "example@example.com", password: "12345699", password_confirmation: "12345699")
@user.confirm!
assert(!@user.save, "this should not be valid since it lacks a zipcode")
@user.update_attributes(password: "12345678", password_confirmation: "12345678")
assert(!@user.save, "this should be invalid straight up")
@user.update_attributes(sex: "male")
@user.update_attributes(zipcode: "33333")
@user.save
puts @user.errors.to_yaml
assert(@user.save, "this should be valid straight up")
end

我得到这个输出:

  Run options: 

  # Running tests:

  --- !ruby/object:ActiveModel::Errors
  base: !ruby/object:User
 attributes:
id: 980190962
username: 
email: ''
encrypted_password: !binary |-
  JDJhJDA0JEFHc0RaOG9zWDlGcWguNGtTN0tuTi5SeEtGNGpqcVR3bU4uVFdD
  VzZ6eWxMVGxFbXJzUmhh
reset_password_token: 
reset_password_sent_at: 
remember_created_at: 
sign_in_count: 0
current_sign_in_at: 
last_sign_in_at: 
current_sign_in_ip: 
last_sign_in_ip: 
created_at: 2013-02-21 19:42:48.000000000 Z
updated_at: 2013-02-21 19:42:48.985150392 Z
first_name: 
last_name: 
zipcode: 33333
sex: male
provider: 
uid: 
avatar_image: 
confirmation_token: GjCHVfywWX2hqUHTqBgN
confirmed_at: 2013-02-21 19:42:49.195009552 Z
confirmation_sent_at: 2013-02-21 19:42:49.035655702 Z
unconfirmed_email: example@example.com
messages: !omap
- :email:
- can't be blank
- can't be blank
  F

 Finished tests in 2.479095s, 0.4034 tests/s, 1.2101 assertions/s.

1) Failure:
test_does_not_validate_without(UserTest) [test/unit/user_test.rb:105]:
this should be valid straight up

是什么赋予了??即使我确认了,为什么电子邮件是空白的?如果我在更新邮政编码之前将确认调用移动到,但在更新性别之后,即使我验证性别和邮政编码都存在,这仍然有效(测试运行没有错误并且全部通过)!我目前没有测试宝石。

这是我的模型的相关部分:

  class User < ActiveRecord::Base

     devise :database_authenticatable, :confirmable, :registerable,:omniauthable, :recoverable, :rememberable, :trackable, :validatable

# Setup accessible (or protected) attributes for your model
attr_accessible :username, :sex, :email, :password, :first_name, :last_name, :zipcode,
              :password_confirmation, :remember_me, :provider, :uid, :avatar_image, :remote_avatar_image_url


validates :username, :uniqueness => true
validates :email, :uniqueness => true
validates_presence_of :sex, :zipcode, :password_confirmation
validates_presence_of :email
validates_length_of :zipcode, :is => 5
# attr_accessible :title, :body
has_one :gallery, :dependent => :destroy


end
4

1 回答 1

0

我想你不能打电话确认!除非用户是有效的

是确认文件!方法 :

# Confirm a user by setting it's confirmed_at to actual time. If the user
# is already confirmed, add an error to email field. If the user is invalid
# add errors
于 2014-10-22T08:19:01.467 回答