简体   繁体   中英

What's the best way to test mongoid models in rails with tdd/bdd?

To test rails/mongoid DB models until now, I used to have a real model with few data (3 .. 5), trying to cover every case for real.

I'm already testing outside in BDD with cucumber, but I usually stop earlier, using rspec very little and without fixture and mockup, prefearing real cases and printing the fields values with rails logger via Rails.logger.info

But now I fed up with that, when the models get more complicated, it's too overwhelming.

What's the best way to test mongoid models in rails with tdd/bdd ? Where can I start ?

I haven't worked with Mongoid before, but is there any reason why testing those models would be any different than one using ActiveRecord? I personally rely on RSpec for unit testing, which should only be testing the external API (behavior) of your models and should not rely much on fixtures or testing internal data or private methods. You can use mocks & stubs to avoid DB persistence in a lot of cases too.

If your models are getting too complicated, you might want to consider taking a more OOP approach and extracting some logic to other classes which do not rely on Mongoid. You have a lot of options in this case and I highly recommend Refactoring Ruby if you're interested in learning more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM