Ruby on rails RoR:最小测试错误:应为false,否则为true

Ruby on rails RoR:最小测试错误:应为false,否则为true,ruby-on-rails,minitest,Ruby On Rails,Minitest,正在寻找使用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 Expected false to be

正在寻找使用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
Expected false to be truthy.
我不知道我做错了什么?谢谢。

断言东西。有效吗?是Rails教程中推广的测试反模式。这是一种反模式,因为您一次测试每一个验证,并且误报和否定的可能性都很大。错误消息也完全没有告诉您测试失败的原因

相反,如果您想测试验证,请使用

使编写这些测试的难度大大降低。
Expected false to be truthy.
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