Database issues when using RSpec for testing - ruby-on-rails

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.

Related

couldn't run test in a modular rails app

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.

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

rails test database cloning dev database

I'm new to Ruby on Rails and following Michael Hartls Ruby on Rails Tutorial. When I run bundle rake exec test, I am getting this error:
ERROR["test_should_get_new", SessionsControllerTest, 2.331314]
test_should_get_new#SessionsControllerTest (2.33s)
ActiveRecord::StatementInvalid:
ActiveRecord::StatementInvalid: Could not find table 'users'
and once similar for all of my tests. I see that my test database: test.sqlite3 is empty. My development database development.sqlite3 does have the users table in it.
I tried running the following to attempt cloning the users database to the test database, but nothing is working:
rake db:migrate:reset
rake db:migrate db:test:prepare
rake db:test:clone
rake db:test:prepare
rake db:test:load
but it's still empty. Any suggestions please?
I think you might be able to get it with:
RAILS_ENV=test rake db:migrate
Give it a try, I've had this problem before myself. I believe that
rake db:test:prepare
is deprecated. What version of ruby and rails are you using?
I figured out a fix, although I'm still not sure why there was a problem to begin with. What I did was I took the pure Sql CREATE TABLE used to create the development table and executed it in the users table. Now everything is working fine.

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.

Getting: "Migrations are pending; run 'bin/rake db:migrate RAILS_ENV=development' to resolve this issue." after cloning and migrating the project

I cloned my project. Bundled with "bundle install", then run "rake db:migrate". I am getting this error: (when I run the rails server and open my browser to localhost:3000) "Migrations are pending; run 'bin/rake db:migrate RAILS_ENV=development' to resolve this issue."
I checked all migrations one by one and all were executed without errors. Also no errors were shown after the execution of "rake db:migrate".
This is what I see when I execute "rake db:migrate:status"
I am on development environment. Please let me know if you need any other information.
I also tried "bundle exec rake db:migrate", and "bundle exec rake db:migrate:reset" as "burninggramma" suggested.
Any clues what causes the error?
Interesting. Did you run rake db:create? Assuming you are using sqlite3, do this:
rm -f db/*.sqlite3
rake db:create
RAILS_ENV=development bundle exec rake db:migrate
rails s -e development
Also, can you list the contents of your config/database.yml file?
Edit: Warning! Obviously, you will lose your existing data.
After running the migrate command, I still had the same error.
What worked for me was to just stop the rails server and start it again.
List your executed migrations with rake db:migrate:status and look if every migration was executed. You can try to cancel your migration with rake db:abort_if_pending_migrations and try to migrate again.
1. Maybe its default in ruby2/rails4 but have you tried: bundle exec rake db:migrate?
2. Another option would be resetting the whole database - use with CAUTION! resets all the data as well - bundle exec rake db:migrate:reset
+) I would just make sure that you are executing everything in the same development env:
RAILS_ENV=development bundle exec rake db:migrate:reset
RAILS_ENV=development bundle exec rails s
Running rake db:migrate RAILS_ENV=test did it for me
I had the same error in the browser, but upon closely looking at the error message, I noticed some how I had an extra white space in the migrate comment and post files. Once I removed it, it worked perfectly.
Open the database and click schema_migration table. The migrations will be listed as below.
Sort the version column and find the latest migration you want to go back. Delete or Insert a new one. Rails keeps all the migration history in this table, so you can adjust the migration history to you liking.
I got the same error working on the Learn Enough to Be Dangerous Rails tutorial. I'm using Git Bash terminals on a Windows 10 machine. The error showed up in the terminal where I'm running guard, after I tried to migrate my db using the command (in another terminal):
$ bundle exec rake db: migrate:
After trying the solution offered by #lewstherin, I still got the same error. I tried the command:
$ rails test
And got the explicit and helpful warning:
Migrations are pending. To resolve this issue, run:
bin/rails db:migrate RAILS_ENV=test
I ran the command:
$ bin/rails db:migrate RAILS_ENV=test
and now I'm working again.
For me i just had to migrate for the error:
rake db:migrate --trace
By setting false to config.active_record.migration_error in development.rb might make it workable but i wouldn't recommend it.
Here's what worked for me:
-gem install rails -v 4.1.0
Inside Gemfile:
-gem 'rails', '4.1.0'
(replace the newer/older with this)
Do bundle install and update
-bundle install
-bundle update
In your application.rb :
Remove/Comment - config.active_record.raise_in_transactional_callbacks = true
run bundle rake:
-bundle exec rake db:migrate
Refresh your page and the error should be gone.
Two reasons 'db:migrate:reset' did not work for me
1) loosing data
2) we moved from php to rails, so we had an existing DB and the migrations were written on top of it not from the scratch
So What I tried is to update the 'scheema_migrations'(mysql) table with the list of migrations(just version values) that I was really sure were already run on my db(development), this can be lil time consuming process but it works. I would not attempt this on production though.
I'm guessing the error is that you are creating a table that already exists, I had this problem before.
Step 1
look into the error when you rake dv:migrate
Step 2
go to the model where this table is created
Step 3
add drop_table :[YOUR TABLE] right before the create_table :[YOUR TABLE]
Step 2
run rake db:migrate
Step 3
remove the drop_table once the your migration is done
I had the same problem in genieacs and this code helped:
rake db:drop rake db:create rake db:schema:load RAILS_ENV=development
rake db:migrate rails s -e development
You can always run rake db:reset

Resources