Skip to content

Instantly share code, notes, and snippets.

@BrianSigafoos
Last active August 17, 2017 20:14
Show Gist options
  • Save BrianSigafoos/9c6d41d288e1af870c683fd034ef98f6 to your computer and use it in GitHub Desktop.
Save BrianSigafoos/9c6d41d288e1af870c683fd034ef98f6 to your computer and use it in GitHub Desktop.
Testing syntax, using Shoulda matchers

Fixtures

DEFAULTS: &DEFAULTS
  payment_trail:      $LABEL
  identifier:         $LABEL_stripe_key
  amount_cents:       <%= AMOUNT_CENTS[:one_h] %>
  paid:               false
  status:             <%= Charge::STATUSES[0] %>
  state:              <%= Charge::STATES[0] %>
  created_at:         now  # special `now` or can use <%= 1.week.from_now %>
  currency:           usd  # refers to currencies(:usd)

invoice:
  <<: *DEFAULTS
  
paid:
  <<: *DEFAULTS
  paid: true

Tests

require 'test_helper'

class ChargeTest < ActiveSupport::TestCase
  should validate_presence_of(:identifier)
  should validate_uniqueness_of(:identifier)
  should validate_inclusion_of(:state).in_array(Charge::STATES)
  should validate_inclusion_of(:status).in_array(Charge::STATUSES)
  should validate_presence_of(:amount_cents)
  validate_numericality_of(:amount_cents).only_integer.is_greater_than_or_equal_to(0)

  test 'scopes' do
    assert_includes Charge.paid, charges(:paid)
  end
  
  test 'validate #custom_validator_method' do
    item = items(:item)
    assert item.valid?

    item.user = item.invitee
    refute item.valid?
    refute errors.keys.include?(:xyz)
    assert.errors.keys.include?(:abc)
    assert_equal item.errors.messages, base: ["Sorry, but you can't accept your own invite"]
  end
end  
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment