mercredi 24 avril 2019

Minitest Model Test Failing

I have a model 'Policy'. Within that model, I have presence validations for policy_holder and premium_amount. I'm attempting to write a MiniTest test for this model (I'm not very familiar with MiniTest). For some reason, my tests are failing. Here is my model:

class Policy < ApplicationRecord
  belongs_to :industry
  belongs_to :carrier
  belongs_to :agent

  validates :policy_holder,  presence: true
  validates :premium_amount, presence: true
end

Here is my controller:

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

And here are my tests:

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

Here is the failure message:

Failure:
PolicyTest#test_should_be_valid_when_both_policy_holder_and_premium_amount_are_present [/Users/shawnk
earney/Desktop/boldpenguin/agent-match-api/test/models/policy_test.rb:22]:
Expected false to be truthy.

The last test is failing when I believe is should be passing. This has me thinking that my other tests are not correct either.

Aucun commentaire:

Enregistrer un commentaire