Patrick J. Collins
2012-Aug-02 20:24 UTC
[rspec-users] "config.before(:each)" is not run after "let!" ?
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? Thank you. Patrick J. Collins http://collinatorstudios.com
David Chelimsky
2012-Aug-02 21:05 UTC
[rspec-users] "config.before(:each)" is not run after "let!" ?
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
Patrick J. Collins
2012-Aug-02 22:29 UTC
[rspec-users] "config.before(:each)" is not run after "let!" ?
> 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 > endHmm.. The way that I usually work is that my "lets" are specific to a spec... # some_spec.rb let!(:xyz) { "XYZ" } # some_other_spec.rb let!(:foo) { "foobar!" } ... So, my goal is to not have to clutter my actual spec files with things like ActionMailer::Base.deliveries.clear ... That''s something that I want to always assure is blank at the beginning of an example regardless of the what spec file I am running. I was hoping there was some way to change the order of hook execution, and make the stuff in spec_helper happen last... Patrick J. Collins http://collinatorstudios.com
David Chelimsky
2012-Aug-02 22:33 UTC
[rspec-users] "config.before(:each)" is not run after "let!" ?
On Thu, Aug 2, 2012 at 6:29 PM, Patrick J. Collins <patrick at collinatorstudios.com> wrote:>> 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 > > Hmm.. The way that I usually work is that my "lets" are specific to a > spec... > > # some_spec.rb > > let!(:xyz) { "XYZ" } > > # some_other_spec.rb > > let!(:foo) { "foobar!" } > > ... > > So, my goal is to not have to clutter my actual spec files with things > like ActionMailer::Base.deliveries.clear ... That''s something that I > want to always assure is blank at the beginning of an example regardless > of the what spec file I am running. > > I was hoping there was some way to change the order of hook execution, > and make the stuff in spec_helper happen last...Nope. Global befores will always run before the ones declared locally.