I am following through a simple tutorial and running into the following issue;
Task.create task: 'This is my task'
Returns an error when rspec tries to run it;
ActiveRecord::StatementInvalid:
Could not find table 'tasks'
But when I call the exact same line from the rails console or from a controller the task is created and I am able to see the new row from within the rails console.
Initially I thought it was maybe something going weird with guard, because I have noticed a few odd things (Ctrl+C doesn't kill it for one) but I decided to run the test directly using rspec and it returns the same result.
Any help would be greatly appreciated.
You have to set up and prepare your db first and you can do that by running rake db:test:prepare
Related
I created a model with a column named errors and when I ran the test I naturally got an ActiveRecord::DangerousAttributeError
so I rolled back the migration and changed the column name to error_messages and reran the migration. I don't refer to that column yet in any other part of my code.
Now I can create valid objects from this model in the rails console, but still the test is giving the same error. How do I make this error go away?
I needed to refresh the database structure in the testing environment like this:
bundle exec rake db:reset RAILS_ENV=test
I am trying to call rake task in one of my methods of controller. The action is getting executed the method is also getting redirected but my rake task is not working. I have tried everything system, backticks, calling another model method etc. But its not at all working. And its happening only in production in development it works fine. I have no clue what is wrong. Thanks in advance.
`rake maintenance:sidekiq:print`
or
system('rake maintenance:sidekiq:print')
Not sure how you are calling the task, but there are couple of ways
system "rake task_name"
Else you can do something like
require 'rake'
Rake::Task["task_name"].invoke
It works for me .
New to the rails world. Using 4.2.4.
I'm trying to create a model for my app and write some unit tests for it, but I'm running into difficulty after running a change migration. I created a model with
rails generate model player first_name:string last_name:string dispaly_name:string
and ran rake db:migrate RAILS_ENV=test
Then I wrote a (failing) unit test to make sure the proper fields were set when calling save. At this point, I realized that I misspelled dispaly_name, so I created a change migration that fixed the column name.
Now when I try to run the unit test rake test test/models/player_test.rb, I get an error (UndefinedColumn) that says the save failed because the Player model is still trying to save with the dispaly_name instead of display_name. I ran rake db:migrate rake db:migrate RAILS_ENV=test rake db:test:prepare and ran rails c and RAILS_ENV=test rails c and checked that the column name was set properly by running Player.column_names. I also checked schema.rb. Everything seems to be in order, but I can't figure out why the Player.new in my test case is using the old column name.
Can someone explain what's happening?
I got it:
when you create model then a fixture along with test file is created.
you created your model with field name dispaly_name, and in file YourApp/test/fixtures/player_test.yml a field dispaly_name is created. right?.
but the problem is that when you change field name to display_name it changes in MODEL and TABLE too but not in fixture file. so you need to correct your field in fixture file too.
please change field name dispaly_name in file YourApp/test/fixtures/player_test.yml
I hope it will work :)
Depending upon what you are using to populate your test db, FactoryGirl, Fabrication or the default Fixtures a Rails application come up with, change the data field name there. Then the error will disappear.
Do try restarting the server just in case if the error persists. I dont think so restarting the server will have any effect but just for a try.
Try to run bundle exec rake:test:clone, this recreates test db schema from current development one.
I'm doing an Rspec feature to test some user story and I'm getting the error message:
Internal Server Error uninitialized constant Tree::MY_BRAnCH
Now, I know the test fails because the table "trees" doesn't have the proper rows but only fails when I run the suite test.
RAILS_ENV=test bundle exec rspec spec/
pointing to the articles_spec.rb file as responsible. But if I run just the feature file:
RAILS_ENV=test bundle exec rspec spec/features/articles_spec.rb
the test pass fine. Digging in the code I see other developer made a test with the indication:
before { truncate(Tree) }
So that test is executed first and is removing the data in the table.
My question is: how can avoid this? Need I to reload all the database before each rspec file?
or what policy should we follow to be sure the rspec tests are not affecting other developers?
It seems unlikely that truncating a database table would cause an uninitialized constant error. More likely, articles_spec.rb causes Tree::MY_BRAnCH to be defined. That's why running articles_spec.rb alone passes. When you run the whole spec suite, something tries to use the constant before it has been defined, hence uninitialized constant.
One solution could be to search your codebase for usages of Tree::MY_BRAnCH and make sure that it has been defined before it is used. You may need to learn about one or more of the following code loading techniques:
Kernel#require
Kernel#autoload
ActiveSupport::Autoload
I am trying to create some functional tests around my controllers but when I attempt to create the test db I get the error described in my thread. This is the first time I have attempted to populate a test db. I am using the default sqlite adapter. I have populated my fixtures with some test values. Can someone tell me what this error means?
Thanks!
Apparently I needed to specifiy the environment to use. This command did the trick: rake db:test:load RAILS_ENV="test"