RoR: Minitest error: Expected false to be truthy
RoR: Minitest error: Expected false to be truthy
寻求帮助使用 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
当我运行测试时:rake test TEST=test/models/car_test.rb
Expected false to be truthy.
我不知道我做错了什么?谢谢
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
end
寻求帮助使用 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
当我运行测试时:rake test TEST=test/models/car_test.rb
Expected false to be truthy.
我不知道我做错了什么?谢谢
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
end