Thanks guys, I've got the teardown working now using DatabaseCleaner in 
truncate mode... provided I have loaded the schema.  Later I'll write a 
test:prepare task that does what ActiveRecord does and clone the development 
database schema into the test database (and then run any migrations on the test 
database... since you can't do test-first development if your migrations have 
to be run on the development database first).

I'm not sure why auto_migrate isn't doing anything for me.  I turn on debug 
mode and the only queries it executes are the select/insert, but nothing do 
with any migration.

This should produce fast tests, I assume:

task ':test:prepare' => [':db:test:clone_structure', ':db:test:migrate']

Since :test:prepare is only executed before all the tests are run (as opposed 
to before each test), the overhead of this initial setup shouldn't be much.


El 11/05/2011, a las 02:53, Ian MacLeod escribió:

> So, re: the worries about nested transactions - totally fair!  You can
> also configure database_cleaner to just truncate all the model tables
> after each test - take a look at the docs for it!
> 
> My general test set up is to run auto_migrate! on test start (making
> sure to have loaded in any models defined for tests before it).  I'd
> try very hard to avoid running auto_migrate! any more often - it's
> extremely expensive and will dramatically slow down your tests.
> 
> Note, that if you use Spork, you'll probably want to run
> DataMapper.auto_migrate! in the Spork.each_run block so that your
> models are fresh each time (but this can add a good chunk of latency
> before your tests can start running)
> 
> If you want to migrate the test environment from the command line, you
> can pass the rails environment as an env var (There might be a less
> awkward way; haven't found it yet):
> 
> RAILS_ENV=test rake db:automigrate
> 
> (I prefer the manual route so I can iterate faster on my tests.)
> 
> -Ian
> 
> On May 10, 5:10 am, Chris Corbyn <[email protected]> wrote:
>> So, manually wiping my test database now just means that the tests fail 
>> because the tables don't exist.  Calling DataMapper.auto_migrate! in my 
>> setup doesn't seem to do anything.  I Googling for "DataMapper Rails unit 
>> testing" doesn't seem to turn up anything useful.  There's no section in the 
>> documentation that talks about testing.  Would anybody mind sharing their 
>> approach to writing unit/functional tests for their Rails 3 applications 
>> that use DataMapper?  I'm guessing this just isn't accounted for in the 
>> dm-rails project?  Ian, how is your test schema kept up-to-date... do you 
>> just use "rake db:automigrate" when you've modified your schema?
>> 
>> There only seems to be a rake task to nuke my development (and as a 
>> side-effect my test) database... not one to just reload the test DB.
>> 
>> El 10/05/2011, a las 16:49, Ian MacLeod escribió:
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>>> I can't directly answer why it's not tearing down your test database,
>>> but I've had a lot of luck usinghttps://github.com/bmabey/database_cleaner
>>> to ensure a clean slate between each test - and it's quick about it!
>>> It starts a transaction at the start of a test, and rolls back when
>>> the test is over
>> 
>>> -Ian
>> 
>>> On May 9, 11:43 pm, Chris Corbyn <[email protected]> wrote:
>>>> Hi All,
>> 
>>>> I'm not sure what I'm missing, but I had a project that started with 
>>>> ActiveRecord, which I then subsequently swapped to use DataMapper, by 
>>>> adding the gem and only requiring selected parts of rails into my 
>>>> application.rb.
>> 
>>>> I have these included in application.rb:
>> 
>>>> require "action_controller/railtie"
>>>> require "action_mailer/railtie"
>>>> require "rails/test_unit/railtie"
>> 
>>>> When I run my unit tests, any data inserted into the database during the 
>>>> test is left there, dirtying the state for the next test.  I can't see 
>>>> what I've missed, comparing my project with a blank one created using:
>> 
>>>> rails new datamapper_project -mhttp://datamapper.org/templates/rails.rb
>> 
>>>> Can anyone shed any light on this?  I had disabled fixtures (because I 
>>>> don't use them), but this should affect it.  Oddly, when I put "fixtures 
>>>> :all" back in the test_helper.rb, the test runner crashes with:
>> 
>>>> /home/ccorbyn/projects/flippa-rails/test/test_helper.rb:6:in 
>>>> `<class:TestCase>': undefined method `fixtures' for 
>>>> ActiveSupport::TestCase:Class (NoMethodError)
>>>>         from /home/ccorbyn/projects/flippa-rails/test/test_helper.rb:5:in 
>>>> `<top (required)>'
>>>>         from test/unit/user_test.rb:1:in `require'
>>>>         from test/unit/user_test.rb:1:in `<top (required)>'
>>>>         from 
>>>> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/lib/rake/rake_test_load
>>>>  er.rb:5:in `load'
>>>>         from 
>>>> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/lib/rake/rake_test_load
>>>>  er.rb:5:in `block in <main>'
>>>>         from 
>>>> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/lib/rake/rake_test_load
>>>>  er.rb:5:in `each'
>>>>         from 
>>>> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/lib/rake/rake_test_load
>>>>  er.rb:5:in `<main>'
>> 
>>>> Surely projects without AR still have their test databases torn down after 
>>>> each test, right?
>> 
>>>> class ActiveSupport::TestCase
>>>>   fixtures :all
>>>> end
>> 
>>>> Here's a little test showing the issue:
>> 
>>>> class UserTest < ActiveSupport::TestCase
>>>>         test "user should be created" do
>>>>                 user = User.new
>>>>                 user.username = "testing"
>>>>                 user.full_name = "Test User"
>>>>                 assert user.save, "User should be saved"
>>>>         end
>> 
>>>>         test "no users should exist by default" do
>>>>                 assert_equal 0, User.count, "No users should exist by 
>>>> default"
>>>>         end
>>>> end
>> 
>>>> Which fails with:
>> 
>>>>   1) Failure:
>>>> test_no_users_should_exist_by_default(UserTest) 
>>>> [test/unit/user_test.rb:12]:
>>>> No users should exist by default.
>>>> <0> expected but was
>>>> <69>.
>> 
>>>>   2) Error:
>>>> test_user_should_be_created(UserTest):
>>>> DataObjects::IntegrityError: Duplicate entry 'testing' for key 2
>>>>     
>>>> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/dm-do-adapter-1.1.0/lib/dm-do-adap
>>>>  ter/adapter.rb:114:in `execute_non_query'
>>>>     
>>>> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/dm-do-adapter-1.1.0/lib/dm-do-adap
>>>>  ter/adapter.rb:114:in `block (2 levels) in create'
>>>>     
>>>> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/dm-do-adapter-1.1.0/lib/dm-do-adap
>>>>  ter/adapter.rb:276:in `with_connection'
>>>>     
>>>> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/dm-do-adapter-1.1.0/lib/dm-do-adap
>>>>  ter/adapter.rb:113:in `block in create'
>>>>     
>>>> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/dm-do-adapter-1.1.0/lib/dm-do-adap
>>>>  ter/adapter.rb:85:in `each'
>>>>     
>>>> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/dm-do-adapter-1.1.0/lib/dm-do-adap
>>>>  ter/adapter.rb:85:in `create'
>>>>     
>>>> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/dm-core-1.1.0/lib/dm-core/reposito
>>>>  ry.rb:146:in `create'
>>>>     
>>>> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/dm-core-1.1.0/lib/dm-core/resource
>>>>  /state/transient.rb:61:in `create_resource'
>>>>     
>>>> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/dm-core-1.1.0/lib/dm-core/resource
>>>>  /state/transient.rb:25:in `commit'
>>>>     
>>>> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/dm-core-1.1.0/lib/dm-core/resource
>>>>  .rb:957:in `_persist'
>>>>     
>>>> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/dm-core-1.1.0/lib/dm-core/resource
>>>>  .rb:971:in `block in create_with_hooks'
>>>>     
>>>> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/dm-core-1.1.0/lib/dm-core/resource
>>>>  .rb:968:in `catch'
>>>>     
>>>> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/dm-core-1.1.0/lib/dm-core/resource
>>>>  .rb:968:in `create_with_hooks'
>>>>     
>>>> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/dm-core-1.1.0/lib/dm-core/resource
>>>>  .rb:1022:in `save_self'
>>>>     
>>>> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/dm-core-1.1.0/lib/dm-core/resource
>>>>  .rb:1007:in `block in _save'
>>>>     
>>>> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/dm-core-1.1.0/lib/dm-core/resource
>>>>  .rb:1223:in `run_once'
>>>>     
>>>> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/dm-core-1.1.0/lib/dm-core/resource
>>>>  .rb:1006:in `_save'
>>>>     
>>>> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/dm-core-1.1.0/lib/dm-core/resource
>>>>  .rb:406:in `save'
>>>>     test/unit/user_test.rb:8:in `block in <class:UserTest>'
>> 
>>>> Something really weird is happening.  That number <69> goes up by one 
>>>> every time I run this test.
>> 
>>>> Cheers,
>> 
>>>> Chris
>> 
>>> --
>>> You received this message because you are subscribed to the Google Groups 
>>> "DataMapper" group.
>>> To post to this group, send email to [email protected].
>>> To unsubscribe from this group, send email to 
>>> [email protected].
>>> For more options, visit this group 
>>> athttp://groups.google.com/group/datamapper?hl=en.
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "DataMapper" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/datamapper?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"DataMapper" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/datamapper?hl=en.

Reply via email to