Question about nested context setup / teardown

164 views
Skip to first unread message

Eric Artzt

unread,
Oct 7, 2008, 12:48:24 PM10/7/08
to shoulda
Hi,

I am doing some unit testing using Shoulda. I am creating some test
objects in the DB using a factory approach. My DB objects that I am
creating are not being cleaned up - so they are accumulating. I
thought any "should" block is equivalent to a test and that all the
nested context setups would be run once in preparation, and that the
DB state would start from fresh each execution? That's not whats
happening here

To simplify what I am doing:

class ResponseTest < ActiveSupport::TestCase
def setup
# build some basic objects here using factory - these are created
once
end

context "Build responses" do

setup do
# some other setup stuff here that does not populate the DB
end

context "create five responses" do
setup do
# create five "Response" DB objects
end

should "have created the responses" do
# puts Response.count
# There are five DB objects at this point - good
end

context "and check :tom / :dietary response" do
setup do
# Some DB queries here
end

should "have created response" do
# puts Response.count
# Now there are 10 DB objects - I would expect 5
end

end

end

end
end

david.l...@gmail.com

unread,
Oct 7, 2008, 1:41:35 PM10/7/08
to sho...@googlegroups.com
I've noticed a similar problem with using factories...
One way around it is to make blank fixture files, and do
"fixtures :all", and turn transactional fixtures on.
Curious to hear of any other ideas/solutions.

-David

Michael Breen

unread,
Oct 7, 2008, 1:51:20 PM10/7/08
to sho...@googlegroups.com
Adding the line"self.use_transactional_fixtures = true" in
test_helper.rb should do the trick. I don't have any fixtures in my
app app and this works fine.

Best.
Mike

david.l...@gmail.com

unread,
Oct 7, 2008, 2:28:57 PM10/7/08
to sho...@googlegroups.com
I'm sure the tests works fine, but have you verified that it actually
clears out the database when it's done?

-D

Tammer Saleh

unread,
Oct 7, 2008, 2:31:55 PM10/7/08
to sho...@googlegroups.com
Hi David,

That's not how transactional fixtures work. All they ensure is that
the DB is set to the original state after each test run. They do not
remove data beforehand, and if there is data in your test DB before
the test, it will still be there afterward.

Cheers,
Tammer
---------------------
www.tammersaleh.com
www.thoughtbot.com
(626) 841-0708

Michael Breen

unread,
Oct 7, 2008, 2:50:46 PM10/7/08
to sho...@googlegroups.com
Yup.

Here's how I verified it:

rake db:reset RAILS_ENV=test

I then checked the users table in the database app_test via dbconsole
and it's empty.

rake test (all tests pass)

I then rechecked the user table in app_test database and it's still
empty.

If I perform the above steps after commenting out
"self.use_transactional_fixtures = true" I get 82 records in the user
table.

Best.
Mike

On Oct 7, 2008, at 2:28 PM, david.l...@gmail.com wrote:

Eric Artzt

unread,
Oct 9, 2008, 5:03:15 PM10/9/08
to shoulda
I figured out what the problem is. I am using the MyISAM engine for a
couple of the mySQL tables - so the transactional fixtures don't work.
So obvious. I am working around it with script. Thanks everyone.

Eric Artzt

unread,
Oct 9, 2008, 5:03:27 PM10/9/08
to shoulda
I figured out what the problem is. I am using the MyISAM engine for a
couple of the mySQL tables - so the transactional fixtures don't work.
So obvious. I am working around it with script. Thanks everyone.

On Oct 7, 9:48 am, Eric Artzt <EricAr...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages