[rspec-users] "config.before(:each)" is not run after "let!" ?

David Chelimsky dchelimsky at gmail.com
Thu Aug 2 21:05:48 UTC 2012


On Thu, Aug 2, 2012 at 4:24 PM, Patrick J. Collins
<patrick at collinatorstudios.com> wrote:
> I have a model with an observer that emails out upon creation, and I
> want to do some testing of emails that get generated via various
> actions...  So I was hoping I could do:
>
> # spec_helper.rb
>
> config.before(:each) do
>   ActionMailer::base.deliveries.clear
> end
>
> # my_phat_observer_spec.rb
>
> let!(:yo_momma) { create_yo_momma }
>
> describe "after_update" do
>   it "sends an email when it knows your daddy" do
>     expect { yo_momma.update_attribute(:daddy => create_daddy) }.to
> change(ActionMailer::Base, :deliveries).by(1)
>   end
> end
>
> ...
>
>
> However, inside that example group, ActionMailer::Base.deliveries is populated from the create_yo_momma
> method-- before the expectation is even declared.
>
> Is there a way to make a config.before(:each) that actually runs as the
> *LAST* before callback prior to the actual example group?

let! adds a before hook and before hooks get run in the order they're
declared, so just declare them in the other order:

let!(:yo_momma) { create_yo_momma }
config.before(:each) do
  ActionMailer::base.deliveries.clear
end

HTH,
David


More information about the rspec-users mailing list