首先,我要说,我对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这是密码
user_test
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
endpost_test
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
enduser.rb
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
endpost.rb
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发布于 2018-05-14 15:08:14
由于您没有显示任何关于User模型的信息,所以不可能知道为什么它是无效的。但是,在你的测试中,你可以做这样的事情:
puts @user.errors.full_messages看看问题出在哪里。
在第二个错误中,错误消息会准确地告诉您什么是错误。您不是在new中传递一个new,而是传递一个符号:
def setup
@post=Post.new(:ruby_meetup)
end:ruby_meetup应该类似于:
ruby_meetup: 'some_value'发布于 2018-05-14 20:17:16
这个错误已经解决了。我把这段代码放在我的user_test.rb中
assert @user.valid?, @user.errors.full_messages修正了错误,我把密码空了。
谢谢大家的帮助,我真的很感激。
https://stackoverflow.com/questions/50333462
复制相似问题