Testing classes that can't be instantiated #147

Closed
danielfarrell opened this Issue Dec 20, 2010 · 2 comments

Projects

None yet

2 participants

@danielfarrell

Working in Rails3 and writing a mailer test we ran across an error where shoulda was trying to instantiate the mailer class, which has a private new method. We fixed it here by overriding get_instance_of in Shoulda::InstanceMethods as follows:

module Shoulda
module InstanceMethods
def get_instance_of(object_or_klass) # :nodoc:
if object_or_klass.is_a?(Class) && object_or_klass.public_methods.include?(:new)
object_or_klass.new
else
object_or_klass
end
end
end
end

@jferris
Member
jferris commented Dec 21, 2010

If you need to test an instance of a class that can't be instantiated, you can provide an explicit subject to your context:

subject { MailerClass.whatever_method_builds_the_mailer }
#  ...
should send_have_send_email

If you need to test the class itself, you can provide the class as the subject. In general, I think it makes sense for shoulda to assume that you can instantiate a class.

@danielfarrell

I agree with that in principal. Writing mailer tests is gonna be a common use case for shoulda though, so at least documenting this would seem like a good idea.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment