Skip to content

Instantly share code, notes, and snippets.

@anthonycrumley
Created July 18, 2013 14:27
Show Gist options
  • Save anthonycrumley/6029778 to your computer and use it in GitHub Desktop.
Save anthonycrumley/6029778 to your computer and use it in GitHub Desktop.
Issues with Shoulda and domain model unit tests.
require 'test_helper'
class GF::Appointments::FooTest < ActiveSupport::TestCase
# In non-ActiveRecord classes that use Shoulda a subject must be
# defined to use the should dsl method. Otherwise it will try to
# instantiate a class based on the class name of the test, in this
# case it tries to instantiate GF::Appointments::Foo. This instantiation
# fails if the initializer has required parameters and may have other
# undesirable side effects.
subject { Object.new }
context "This test" do
setup do
# The following clears out the sent emails queue and must be done
# in the setup of tests that check for emails.
ActionMailer::Base.deliveries = []
# Do something that sends an email...
end
# This always passes because have_sent_email returns a
# Shoulda matcher like the following which is truthy...
#
# puts have_sent_email.inspect
# <Shoulda::Matchers::ActionMailer::HaveSentEmailMatcher:0x10b96cd58 ... >
should "send an email" do
assert have_sent_email("for advisor").
to("[email protected]").
with_subject(/Appointment Cancelled/)
end
# This may raise an exception unless the subject is set as demonstrated above.
should have_sent_email("for advisor").
to("[email protected]").
with_subject(/Appointment Cancelled/)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment