[rspec-devel] names for setup and teardown methods

David Chelimsky dchelimsky at gmail.com
Tue May 1 23:36:44 EDT 2007


On 5/1/07, Matt Aimonetti <mattaimonetti at gmail.com> wrote:
> Ok, I created a new project and tried to reproduce my issue and I discovered
> something else. I can't really reproduce the previously reported problem.
>
> I have a new rails project with one User model with validates_uniqueness_of
> :email
>
> Here is my spec file for the model
>
> require File.dirname(__FILE__) + '/../spec_helper'
> def create_test_user
>   User.new(:first_name => 'John', :last_name => 'Doe', :email => '
> john at doe.fr').save
> end
>
> describe "A user" do
>
>   it "should be created if valid" do
>     create_test_user.should be(true)
>     create_test_user.should be(false)
>   end
>
>   it "should not have the same email than another user" do
>    create_test_user.should be(false)
>   end
> end
>
> The second example fails. I'm a bit surprised that the user created by the
> first example doesn't exist anymore when the 2nd example tries creating a
> new user.

Actually, it is a well established guideline in unit testing that
every test method (it block in our case) should run in complete
isolation from every other test method. In the case of Spec::Rails,
the database is rolled back to its previous state between each example
- so it is the intended behaviour that your second example should
fail.

Think of the alternative. If that example passed only because of state
set up in the previous example, then you'd not be able to run only
that one example, or run them in a different order. This sort of
independence is absolutely crucial to the process.

>
> rspec_on_rails plugin and rspec gem from trunk. (updated today)
>
>
> -Matt
>
>
> On 5/1/07, aslak hellesoy < aslak.hellesoy at gmail.com> wrote:
> > On 5/1/07, aslak hellesoy < aslak.hellesoy at gmail.com> wrote:
> > > On 5/1/07, Matt Aimonetti <mattaimonetti at gmail.com> wrote:
> > > > after(:each) doesn't work either.
> > > >
> > >
> > > Really? A sample spec showing how after(:each) doesn't work would be
> helpful.
> > >
> >
> > I mean some code I can run to reproduce what you see.
> >
> > > Aslak
> > >
> > > > -Matt
> > > >
> > > >
> > > > On 5/1/07, aslak hellesoy <aslak.hellesoy at gmail.com> wrote:
> > > > >
> > > > > It's after(:each) and not teardown(:each)
> > > > >
> > > > > On 5/1/07, Matt Aimonetti < mattaimonetti at gmail.com> wrote:
> > > > > > describe "a legacy user" do
> > > > > >   include LegacyUserHelper
> > > > > >
> > > > > >   before :all do
> > > > > >     LEGACY.connect
> > > > > >     migration_for_legacy_users
> > > > > >   end
> > > > > >
> > > > > >   before do
> > > > > >     @l_user = LegacyUser.new
> > > > > >   end
> > > > > >
> > > > > >  teardown :each do
> > > > > >
> > > >
> delete_user_with_email(valid_legacy_user_attributes[:email])
> > > > > >  end
> > > > > >
> > > > > > Here is a snippet of my code.
> > > > > >
> > > > > > Before running my examples, I need to connect to my legacy
> database and
> > > > > > migrating the schema. After each example I need to delete the
> legacy
> > > > user
> > > > > > just created. To get the above code to work, I had to move my
> > > > > >
> > > >
> delete_user_with_email(valid_legacy_user_attributes[:email])
> > > > function to be
> > > > > > run before each example instead of after.
> > > > > >
> > > > > > -Matt
> > > > > >
> > > > > > On 4/30/07, David Chelimsky < dchelimsky at gmail.com> wrote:
> > > > > > >
> > > > > > > On 4/30/07, Matt Aimonetti < mattaimonetti at gmail.com> wrote:
> > > > > > > > teardown :each and after :each don't seem to work for me.
> > > > > > > >
> > > > > > > > I had to move my function from "teardown :each" to "before"
> since
> > > > the
> > > > > > > > function was deleting the object created by the previous spec
> and
> > > > the
> > > > > > > > function didn't seem to be called. Maybe I didn't understand
> > > > properly
> > > > > > > how
> > > > > > > > teardown and after work.
> > > > > > >
> > > > > > > Each example (i.e. #it or #specify) gets run in its own object.
> It has
> > > > > > > no access to any state in the other examples. before(:each) is
> run
> > > > > > > before each example, in the same object as the example.
> after(:each)
> > > > > > > is run after each example, in the same object as the example.
> > > > > > >
> > > > > > > I'm a bit confused by your description of the problem. Can you
> post
> > > > > > > some code so we can talk about a specific example?
> > > > > > >
> > > > > > >
> > > > > > > >
> > > > > > > > -Matt
> > > > > > > >
> > > > > > > >
> > > > > > > > On 4/18/07, Matthew Heidemann < matthew.heidemann at gmail.com>
> wrote:
> > > > > > > > > I personally like before :each_example. The first time I
> read it I
> > > > > > > said,
> > > > > > > > "before :each what?".
> > > > > > > > >
> > > > > > > > > I think once people run the spec and see 1 example 0
> failures,
> > > > they
> > > > > > > know
> > > > > > > > that the 'it do' is an example
> > > > > > > > >
> > > > > > > > >
> > > > > > > > >
> > > > > > > > > On 4/11/07, Matt Aimonetti < matt at aimonetti.net> wrote:
> > > > > > > > > > I fully agree with Dean and prefer the "before :each",
> "after
> > > > :all"
> > > > > > > > option, it reads better and is easier to understand for
> newbies.
> > > > > > > > > >
> > > > > > > > > > -Matt
> > > > > > > > > >
> > > > > > > > > >
> > > > > > > > > >
> > > > > > > > > > On 4/11/07, Dean Wampler <deanwampler at gmail.com > wrote:
> > > > > > > > > > > Setup and teardown are more approachable to converts
> from
> > > > > > > Test::Unit,
> > > > > > > > > > > but "before :all" and "after :each" read better, IMHO.
> > > > > > > > > > >
> > > > > > > > > > > "all_examples" might cause people to ask "what
> examples?" I'm
> > > > not
> > > > > > > sure
> > > > > > > > > > > most people will think of the "it blocks" as examples.
> Still,
> > > > you
> > > > > > > > > > > could just accept any symbol that begins with "each" and
> > > > "all", so
> > > > > > > > > > > people can invent their own name ;)
> > > > > > > > > > >
> > > > > > > > > > > dean
> > > > > > > > > > >
> > > > > > > > > > >
> > > > > > > > > > > On 4/11/07, David Chelimsky < dchelimsky at gmail.com >
> wrote:
> > > > > > > > > > > > We're going to rename context_setup and
> context_teardown
> > > > because
> > > > > > > > they
> > > > > > > > > > > > no longer read well when using "describe/id". There
> are two
> > > > > > > ideas
> > > > > > > > > > > > floating around about this. The first:
> > > > > > > > > > > >
> > > > > > > > > > > > setup :each        #default - works like setup does
> now
> > > > > > > > > > > > setup :all             #replaces context_setup
> > > > > > > > > > > > teardown :each #default - works like teardown does now
> > > > > > > > > > > > teardown :all      #replaces context teardown
> > > > > > > > > > > >
> > > > > > > > > > > > The second uses "before" and "after" in the same way:
> > > > > > > > > > > >
> > > > > > > > > > > > before :each   #default - works like setup does now
> > > > > > > > > > > > before :all        #replaces context_setup
> > > > > > > > > > > > after :each       #default - works like teardown does
> now
> > > > > > > > > > > > after :all            #replaces context teardown
> > > > > > > > > > > >
> > > > > > > > > > > > In both cases, the default is :each, so you can just
> say
> > > > "setup
> > > > > > > do"
> > > > > > > > or
> > > > > > > > > > > > "before do" instead of "setup :each do" or "before
> :each
> > > > do".
> > > > > > > > > > > >
> > > > > > > > > > > > == Examples
> > > > > > > > > > > >
> > > > > > > > > > > > describe Thing do
> > > > > > > > > > > >   setup :all { ... }
> > > > > > > > > > > >   setup :each { ... }
> > > > > > > > > > > >   it "should do something" { ... }
> > > > > > > > > > > >   it "should do something else" { ... }
> > > > > > > > > > > >   teardown :each { ... }
> > > > > > > > > > > >   teardown :all { ... }
> > > > > > > > > > > > end
> > > > > > > > > > > >
> > > > > > > > > > > > describe Thing do
> > > > > > > > > > > >   before :all { ... }
> > > > > > > > > > > >   before :each { ... }
> > > > > > > > > > > >   it "should do something" { ... }
> > > > > > > > > > > >   it "should do something else" { ... }
> > > > > > > > > > > >   after :each { ... }
> > > > > > > > > > > >   after :all { ... }
> > > > > > > > > > > > end
> > > > > > > > > > > >
> > > > > > > > > > > > describe Thing do
> > > > > > > > > > > >   before { ... } # works like before :each
> > > > > > > > > > > >   it "should do something" { ... }
> > > > > > > > > > > >   it "should do something else" { ... }
> > > > > > > > > > > >   after { ... } # works like after :each
> > > > > > > > > > > > end
> > > > > > > > > > > >
> > > > > > > > > > > > One other thing that occurs to me is that "before
> :each"
> > > > really
> > > > > > > > means
> > > > > > > > > > > > "before each example" - so perhaps it should be
> "before
> > > > > > > > :each_example"
> > > > > > > > > > > > and "before :all_examples" (or :each and :each_example
> could
> > > > > > > both be
> > > > > > > > > > > > supported, etc).
> > > > > > > > > > > >
> > > > > > > > > > > > WDYT?
> > > > > > > > > > > >
> > > > _______________________________________________
> > > > > > > > > > > > rspec-devel mailing list
> > > > > > > > > > > > rspec-devel at rubyforge.org
> > > > > > > > > > > >
> > > > http://rubyforge.org/mailman/listinfo/rspec-devel
> > > > > > > > > > > >
> > > > > > > > > > >
> > > > > > > > > > >
> > > > > > > > > > > --
> > > > > > > > > > > Dean Wampler
> > > > > > > > > > > http://www.objectmentor.com
> > > > > > > > > > > http://www.aspectprogramming.com
> > > > > > > > > > > http://www.contract4j.org
> > > > > > > > > > >
> > > > _______________________________________________
> > > > > > > > > > > rspec-devel mailing list
> > > > > > > > > > > rspec-devel at rubyforge.org
> > > > > > > > > > >
> > > > http://rubyforge.org/mailman/listinfo/rspec-devel
> > > > > > > > > > >
> > > > > > > > > >
> > > > > > > > > >
> > > > > > > > > >
> _______________________________________________
> > > > > > > > > > rspec-devel mailing list
> > > > > > > > > > rspec-devel at rubyforge.org
> > > > > > > > > >
> > > > http://rubyforge.org/mailman/listinfo/rspec-devel
> > > > > > > > > >
> > > > > > > > >
> > > > > > > > >
> > > > > > > > >
> _______________________________________________
> > > > > > > > > rspec-devel mailing list
> > > > > > > > > rspec-devel at rubyforge.org
> > > > > > > > >
> http://rubyforge.org/mailman/listinfo/rspec-devel
> > > > > > > > >
> > > > > > > >
> > > > > > > >
> > > > > > > >
> _______________________________________________
> > > > > > > > rspec-devel mailing list
> > > > > > > > rspec-devel at rubyforge.org
> > > > > > > >
> http://rubyforge.org/mailman/listinfo/rspec-devel
> > > > > > > >
> > > > > > > _______________________________________________
> > > > > > > rspec-devel mailing list
> > > > > > > rspec-devel at rubyforge.org
> > > > > > >
> http://rubyforge.org/mailman/listinfo/rspec-devel
> > > > > > >
> > > > > >
> > > > > _______________________________________________
> > > > > rspec-devel mailing list
> > > > > rspec-devel at rubyforge.org
> > > > > http://rubyforge.org/mailman/listinfo/rspec-devel
> > > > >
> > > >
> > > >
> > > > _______________________________________________
> > > > rspec-devel mailing list
> > > > rspec-devel at rubyforge.org
> > > > http://rubyforge.org/mailman/listinfo/rspec-devel
> > > >
> > >
> > _______________________________________________
> > rspec-devel mailing list
> > rspec-devel at rubyforge.org
> > http://rubyforge.org/mailman/listinfo/rspec-devel
> >
>
>
> _______________________________________________
> rspec-devel mailing list
> rspec-devel at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-devel
>


More information about the rspec-devel mailing list