Unit testing ActiveRecord code

8 views
Skip to first unread message

Mike Christensen

unread,
Oct 17, 2009, 8:23:59 PM10/17/09
to castle-pro...@googlegroups.com
When writing unit tests for code that uses ActiveRecord, I want to be
able to call stuff like "Save()" and "Create()" without actually
touching the DB (I just want to no-op).. Is there an easy way to run
AR in "test mode" or do I have to abstract all the calls that actually
touch the DB. Thanks!

Mike

Mauricio Scheffer

unread,
Oct 17, 2009, 9:06:49 PM10/17/09
to Castle Project Users

Markus Zywitza

unread,
Oct 18, 2009, 4:15:08 AM10/18/09
to castle-pro...@googlegroups.com
InMemoryTest is not technically a no-op. A real no-op solution is shown here:

trunk\Samples\Castle\Guide.docx

It's a work in progress and currently stopped, but it contains a no-op testing strategy.

-Markus

2009/10/18 Mauricio Scheffer <mauricio...@gmail.com>

Srdjan Pejic

unread,
Oct 19, 2009, 10:07:22 AM10/19/09
to Castle Project Users
You could also create a test database and run your tests on that.
SetUp and TearDown methods will make sure you don't leave data in the
database.

Srdjan

Brian DeMarzo

unread,
Oct 20, 2009, 9:44:31 AM10/20/09
to Castle Project Users
If you don't want to touch a repository, why call Save() or Create()?
If it's to test business logic related to saving or creating (such as
validation), can you just test that separately?

I've found saving against an actual test SQL database, prepopulated
with data I can use for testing, quite useful, and have a blog post
about how I did it:
http://www.sidesofmarch.com/index.php/archive/2009/01/14/unit-testing-an-activerecord-domain-model-with-nunit/


- b

Markus Zywitza

unread,
Oct 21, 2009, 4:06:37 PM10/21/09
to castle-pro...@googlegroups.com
While testing against a test database is necessary for some scenarios, it requires you to initialize AR per test, which slows down testing. Stubbing AR-methods speeds up testing a lot.

-Markus

2009/10/20 Brian DeMarzo <bdem...@gmail.com>
Reply all
Reply to author
Forward
0 new messages