couldn't run test in a modular rails app - ruby-on-rails

Before I ask my question, I like you to see the structure of my modular rails app, in which apps are engines and plugged into Domain/Parent empty app having only configurations.
See image below:
However, when I run test with rails test or rake test in the domain/parent directory that has nothing aside configuration, I get:
However, I can run rake tasks like bundle install,rake db:migrate etc from the root of the app or domain/parent directory successfully because the engine app is plugged into the root of the app. So it baffles my why rails test or rake test does not work.
I proceeded and cd into engines, into the root app inside the engine and I ran rails test or rake test or rails test test/models/csv_importer/user_test.rb but I get a different thing. See it below:
I did the response but test didn't still run
How do I test a modular rails app? Any help is appreciated.
When I run: rake db:test:prepare from the root of the engine, I get:

Okay, I fix the problem. What I had to do is:
From the root of the engine application (which is csv_importer as shown in the image in the question), I ran the following rails tasks:
bundle exec rake db:drop RAILS_ENV=test
bundle exec rake db:create RAILS_ENV=test
bundle exec rake db:migrate RAILS_ENV=test
Then when I run rails test, my test starts running.

Related

Error when running bin/rails test in Vagrant

I'm running the rails-dev-box on Vagrant, with a folder shared between the box and my Windows computer. Rails version 5.0.5. I have a very basic app using a sqlite3 database, and a basic generated scaffold for a model. When I ran bin/rails test I received this error:
ActiveRecord::Tasks::DatabaseAlreadyExists
A link in this GitHub thread pointed to this SO question, and I followed this answer - I edited database.yml to change the location of the databases to a location outside of the shared folder. I then re-migrated the databases with bin/rails db:migrate. This seemed to help a bit, because the next time I ran bin/rails test I received a different error:
Migrations are pending. To resolve this issue, run:
bin/rails db:migrate RAILS_ENV=test
But even after running bin/rails db:migrate RAILS_ENV=test I still receive this error each time I try to run the test.
I think all you need is bin/rails db:test:prepare before bin/rails test

Creating database & schema before rspec

I have a RoR 5 app which uses MySQL for testing and development. The MySQL server has a development database created, but the testing database is missing (thus specs aren't running).
I could create the database on my own, but I'm wondering if there is a way I can make RoR create it for me when I run rspec spec.
I would do it like this:
RAILS_ENV=test bundle exec rake db:create
RAILS_ENV=test bundle exec rake db:schema:load

How to understand "rake db:seed"

I am using Michael Hartl's rails tutorials.
Whenever I use the following
$ bundle exec rake db:migrate:reset
Then
$ bundle exec rake db:seed
It waits. It doesn't show anything.
And when I do:
bundle exec rake test
I get
ActiveRecord::PendingMigrationError: Migrations are pending.
To resolve this issue,
bin/rake db:migrate RAILS_ENV=test
When the above is done-"db:migrate RAILS_ENV=test", tests are clear.
However Michael doesn't mention anything happening about this scenario, Can anybody help and explain?
By default, most rake commands are going to run in the context of the RAILS_ENV passed to the command line. If no RAILS_ENV is passed to the command line, it will run in the development context, which is separate from the test context. There are a few exceptions, like rake db:create, which will create your development and test databases, but migrate will work against the specified environment.

Database issues when using RSpec for testing

I'm working on a Ruby on Rails project and trying to get RSpec working so i can write some tests. I run the following command:
bundle exec rspec spec/
to run my test. I then get the error
Migrations are pending; run 'rake db:migrate RAILS_ENV=test' to resolve this issue.
so then i run
rake db:migrate RAILS_ENV=test
and get the following error
any help would be appreciated!
The error is misleading. You should never run migrations directly on your test db. Run:
rake db:test:prepare
This post may be helpful depending on your Rails version: Rake db:test:prepare task deleting data in development database
Rails 3.2.9 may solve your issue.

Rails: rake db:test:prepare Vs rake test:prepare

AS the guides, the command used to prepare test database is
bundle exec rake db:test:prepare
However, I have found that following command also works & created the test db for me.
bundle exec rake test:prepare
Wanted to know if this is a valid command, if yes. Where can I find the documentation.
tested on rails 3.2.8
According to github and the source code task 'test:prepare' => 'db:test:prepare' is mentioned at the bottom.
Hence it's a shortcut, wrapper, whatever you want to call it. Another question would be why this doesn't up when you do bundle exec rake -T but hey.

Resources