首先让我说我在Rails中测试相当新。我在测试中遇到了一些错误,我不知道为什么。
rails test:models
Run options: --seed 21021
# Running:
F
Failure:
UserTest#test_user_should_be_valid [/home/ubuntu/workspace/test/models/user_test.rb:9]:
Expected false to be truthy.
bin/rails test test/models/user_test.rb:8
E
Error:
PostTest#test_post_should_be_valid:
ArgumentError: When assigning attributes, you must pass a hash as an argument.
test/models/post_test.rb:5:in `setup'
bin/rails test test/models/post_test.rb:8
Finished in 0.237740s, 8.4126 runs/s, 4.2063 assertions/s.
2 runs, 1 assertions, 1 failures, 1 errors, 0 skips
这是代码
require 'test_helper'
class UserTest < ActiveSupport::TestCase
def setup
@user=User.new(email:"fo0@hotmail.com")
end
test "user should be valid" do
assert @user.valid?
end
end
require 'test_helper'
class PostTest < ActiveSupport::TestCase
def setup
@post=Post.new(:ruby_meetup)
end
test "post should be valid" do
assert @post.valid?
end
end
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :posts
has_many :rsvps
has_many :posts, through: :rsvps
validates :email, presence: true
end
class Post < ApplicationRecord
belongs_to :user
geocoded_by :address
after_validation :geocode, if: ->(obj){ obj.address.present? and obj.address_changed? }
reverse_geocoded_by :latitude, :longitude
after_validation :reverse_geocode
has_many :rsvps
has_many :users, through: :rsvps
validates :name, presence: true
end
答案 0 :(得分:2)
由于您没有显示有关User
模型的任何信息,因此无法确定其无效原因。但是,在您的测试中,您可以执行以下操作:
puts @user.errors.full_messages
看看问题是什么。
在第二个错误上,错误消息告诉您确切的错误。您没有在hash
中传递new
,您传递了一个符号:
def setup
@post=Post.new(:ruby_meetup)
end
:ruby_meetup
应该是这样的:
ruby_meetup: 'some_value'
答案 1 :(得分:0)
错误已经解决。我把这段代码放在我的user_test.rb
中assert @user.valid?, @user.errors.full_messages
并修正了我将密码留空的错误。
谢谢大家的帮助,我很感激。