У меня есть модель «Политика».В этой модели у меня есть проверки присутствия для policy_holder и premium_amount.Я пытаюсь написать тест MiniTest для этой модели.По некоторым причинам мои тесты не проходят.Вот моя модель:
class Policy < ApplicationRecord
belongs_to :industry
belongs_to :carrier
belongs_to :agent
validates :policy_holder, presence: true
validates :premium_amount, presence: true
end
Вот мой контроллер:
class PoliciesController < ApplicationController
def create
policy = Policy.create!(policy_params)
render json: policy
end
private
def policy_params
params.require(:policy).permit(:policy_holder, :premium_amount, :industry_id,
:carrier_id, :agent_id)
end
end
А вот мои тесты:
require 'test_helper'
class PolicyTest < ActiveSupport::TestCase
test 'should validate policy holder is present' do
policy = Policy.find_or_create_by(policy_holder: nil, premium_amount: '123.45',
industry_id: 1, carrier_id: 1,
agent_id: 1)
assert_not policy.valid?
end
test 'should validate premium amount is present' do
policy = Policy.find_or_create_by(policy_holder: 'Bob Stevens', premium_amount: nil,
industry_id: 1, carrier_id: 1,
agent_id: 1)
assert_not policy.valid?
end
test 'should be valid when both policy holder and premium amount are present' do
policy = Policy.find_or_create_by(policy_holder: 'Bob Stevens', premium_amount: '123.45',
industry_id: 1, carrier_id: 1,
agent_id: 1)
assert policy.valid?
end
end
Вот сообщение об ошибке:
Failure:
PolicyTest#test_should_be_valid_when_both_policy_holder_and_premium_amount_are_present [test/models/policy_test.rb:22]:
Expected false to be truthy.
Последний тест не пройден, когда я считаю, что он должен пройти.Это заставляет меня думать, что другие мои тесты тоже не верны.