寻求使用minitest (Rails 5,Ruby 2.7.0)实现简单的第一个测试的帮助
car_test.rb
require 'test_helper'
class CarTest < ActiveSupport::TestCase
test 'valid car' do
car = Car.new(title: 'SALOON', style: '1')
assert car.valid?
end
end我的模型car.rb
class Car < ApplicationRecord
validates :title, :style, presence: true
end当我运行test时: rake test test=test/model/car_test.rb
Expected false to be truthy.我不知道我做错了什么?谢谢。
发布于 2020-03-08 23:57:59
assert thing.valid?是由Rails教程书籍推广的一种测试反模式。这是一种反模式,因为你一次测试每一个验证,并且假阳性和阴性的可能性都很大。错误消息也完全不会告诉您测试失败的原因。
相反,如果您想测试验证,请使用errors object。
require 'test_helper'
class CarTest < ActiveSupport::TestCase
test 'title must be present' do
car = Car.new(title: '')
car.valid?
assert_includes car.errors.messages[:title], "can't be blank"
end
test 'style must be present' do
car = Car.new(style: '')
car.valid?
assert_includes car.errors.messages[:style], "can't be blank"
end
endhttps://stackoverflow.com/questions/60587665
复制相似问题