[rspec-users] "Tricks" for testing after_create callback???

Daniel Tenner daniel.ruby at tenner.org
Thu Dec 13 09:52:28 EST 2007


Then I definitely think there's little point in specifying what  
"deliver_secret_santa" is called with. The chances that you'll get  
that wrong are extremely small, and even if you do it will be picked  
up instantly by your integration tests, and be very easy to fix.

What's much more likely to break is SantasMailbox - so focus the  
time, the effort, and the lines of code on speccing that quite  
thoroughly, so you know it can take every likely Message that you  
might throw at it.

(/opinion)

Daniel

On 13 Dec 2007, at 14:44 13 Dec 2007, Rick DeNatale wrote:

> On 12/13/07, Daniel Tenner <daniel.ruby at tenner.org> wrote:
>> Hi Rick,
>>
>> Could you paste us what your after_save and your deliver_xyz methods
>> look like? I think it would be helpful to make the discussion more
>> concrete.
>>
>> I've found that whenever my code is hard to spec, it's usually poorly
>> designed in the first place, and a better split of responsibilities
>> helps make the code both clearer and easily spec'ed.
>>
>> In this specific case, I'm a bit perplexed that you're sending a pre-
>> generated body/subject/sender/etc to the Mailer. Shouldn't that code
>> be in the Mailer's deliver_xyz method?
>
> In this case the bulk of the message is coming from, surprise, the
> message object.
>
> This is a secret santa site, the idea is to allow folks to communicate
> without giving away who is who.
>
> class Message < ActiveRecord::Base
>
>   belongs_to :sender, :class_name => "Person", :foreign_key  
> => :sender_id
>   belongs_to :recipient, :class_name => "Person", :foreign_key  
> => :recipient_id
>
>   after_create do |msg|
>     puts "\n**** after_create #{msg}\n#{caller.join("\n")}\n"
>
>     SantasMailbox.deliver_secret_santa(msg)
>   end
> ...
> end
>
> class SantasMailbox < ActionMailer::Base
> ...
>   def secret_santa(message)
>     subject     message.subject
>     body(        {:sender => message.sender_name,
>                      :recipient =>  
> message.recipient_first_name, :text => body})
>     recipients  "#{recipient.full_name} <#{recipient.email}>"
>     from        "#{message.sender_full_name}
> <#{message.sender_name}@denhaven2.com"
>     sent_on     message.created_at
>     content_type 'text/html'
>     headers     {}
>   end
> ...
> end
>
> Where methods like sender_name etc. on the Message object produce
> appropriate clear or obfuscated (e.g. 'Your Secret Santa'  depending
> on the message state.
>
> -- 
> Rick DeNatale
>
> My blog on Ruby
> http://talklikeaduck.denhaven2.com/
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users



More information about the rspec-users mailing list