1
votes

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 ?

1

1 Answers

1
votes

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.