Question: How do I get RSpec and Cucumber tests to run in an app using both Mongoid and ActiveRecord?


How do I get RSpec and Cucumber tests to run in an app using both Mongoid and ActiveRecord?

Answers 0
Added at 2016-12-30 16:12

My team's project is a Rails 4.2 app using Mongoid 4. A new feature we are adding requires us to read data from a Postgres DB and use those records in the app through an intermediary object. In other words, our app will play with ActiveModel objects (non-persisted) that contain a mix of data from Mongoid models and ActiveRecord models, and thus can do all the normal cool Rails stuff with Controllers and Views, etc. (We think it's clever.) The intention right now is that we will never write to the SQL tables, just read. Any new data stored will be in mongodb.

For my development environment, the first steps seemed pretty straight-forward:

  • I added pg to the gemfile
  • I added require "active_record/railtie" to application.rb
  • I made my new models in the normal ActiveRecord way (although since I was working with an existing database, I changed the class names to something more meaningful and set the table names accordingly)
  • I added a database.yml file (we didn't need one with mongoid) with all the correct postgres info
  • I created the schema.rb file using rake db:schema:dump. That took a while to figure out I needed it, but I got there.

Using the rails console, I was able to prove to myself that I could search/find objects in both databases. So far so good.

When I first fired up the rspec tests, there were tons of failures related to validation errors because "records already existed with that name". Long investigation story short, I fixed by setting all DatabaseCleaner calls to be DatabaseCleaner[:mongoid], and that got rspec passing again. (I still have no idea what was actually happening behind the scenes.)

BUT, Cucumber (feature) tests? I can't even get them started. It keeps saying:

Migrations are pending. To resolve this issue, run: bin/rake db:migrate RAILS_ENV=test

No! All those migrations we have are for MONGO. There are no SQL/ActiveRecord migrations. Not surprisingly, that rake fails:

rake aborted! ArgumentError: wrong number of arguments (2 for 0).

I tried this series of commands:

rake db:drop RAILS_ENV=test
rake db:create RAILS_ENV=test
rake db:schema:load RAILS_ENV=test

but cucumber still gives the same migration pending error.

So I'm stuck! I haven't found anyone doing what we're trying to do. I know there HAS to be someone, I just haven't found the right search terms, apparently.

The worst part of all this is I had no intention of even using an ActiveRecord test database at all, I was simply going to mock those records in my tests. How can I tell the app, "I only need ActiveRecord for runtime, not testing. Stop looking at the migrations, ActiveRecord, those aren't for you!"

I have run out of ideas and words to search with. I've been trying to solve this problem for a couple of days now and have tried SO many different things, please forgive me if I've left some of those out here. Any assistance would be greatly appreciated.

Answers to

How do I get RSpec and Cucumber tests to run in an app using both Mongoid and ActiveRecord?

Source Show
◀ Wstecz