According to this tutorial http://carlosplusplus.github.io/blog/2014/02/01/testing-rake-tasks-with-rspec/
to test rake tasks with rspec, one has to set
Rake.application = rake
in a before block.
However, I get the error
Failure/Error: Rake.application = rake
NameError:
uninitialized constant Rake
How can I fix this?
You need to require "rake" before the offending code. The tutorial you are using is based on a blog post by Thoughtbot, which includes the appropriate require and can be used as an example.
Related
Here is my console:
ilya#SamsungRV-509:~/MyProjects/easy_learning$ rake db:migrate RAILS_ENV=test
== SetPasswordToAdministrator: migrating =====================================
DEPRECATION WARNING: This dynamic method is deprecated. Please use e.g. Post.find_or_create_by(name: 'foo') instead. (called from up at /home/ilya/MyProjects/easy_learning/db/migrate/20131210185519_set_password_to_administrator.rb:3)
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
rake aborted!
An error has occurred, all later migrations canceled:
undefined method `mobile' for #<User:0xba972894>/home/ilya/MyProjects/easy_learning/db/migrate/20131210185519_set_password_to_administrator.rb:3:in `up'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
Here is my file:
class SetPasswordToAdministrator < ActiveRecord::Migration
def up
admin = ::User.find_or_create_by_email_and_name("admin#email.com", "Admin")
admin.password = admin.password_confirmation = "easylearning"
admin.role = "Administrator"
admin.save!
end
end
You need to run two rake tasks (or ensure they've been run) first to get going with Rspec as it relates to your DB:
rake db:create:all
This ensures that your test DB was created.
rake db:migrate db:test:clone
This will ensure that your test database is both up-to-date and ready for your specs to run.
Over the last year as I've worked to get better about T/BDD and Rspec in particular, I no longer even run rake db:migrate alone. I always run rake db:migrate db:test:clone now!
If you are running Rails 4.1+, RSpec has a feature that keeps your test database synchronised with development. This from the RSpec website:
To take advantage of this add the following to the top of the rails_helper file after Rails has been required:
ActiveRecord::Migration.maintain_test_schema!
Behaviour in Rails 4.0 is slightly different. Here is the page for more information:
https://www.relishapp.com/rspec/rspec-rails/docs/upgrade#pending-migration-checks
I receive the following error, even after doing a rake db:test:prepare. I am running rails 4.0.
1) Core::PostsController GET index assigns all posts as #posts
Failure/Error: post = Post.create! valid_attributes
ActiveRecord::StatementInvalid:
Could not find table 'core_posts'
# ./spec/controllers/core/posts_controller_spec.rb:36:in `block (3 levels) in <module:Core>'
I am running this test inside an engine, so could it be something related? My test looks like this:
module Core
describe PostsController do
# This should return the minimal set of attributes required to create a valid
# Post. As you add validations to Post, be sure to
# adjust the attributes here as well.
let(:valid_attributes) { { } }
# This should return the minimal set of values that should be in the session
# in order to pass any filters (e.g. authentication) defined in
# PostsController. Be sure to keep this updated too.
let(:valid_session) { {} }
describe "GET index" do
it "assigns all posts as #posts" do
post = Post.create! valid_attributes
get :index, {}, valid_session
assigns(:posts).should eq([post])
end
end
end
end
Any ideas? Thanks!
cd into the engine dir & generate a dummy app for testing for your engine:
rails plugin new . --full --mountable --dummy-path spec/dummy
the above command will generate a full mountable engine with isolated namespace, meaning that all the controllers and models from this engine will be isolated within the namespace of the engine. For instance, the Post model later will be called Core::Post, and not simply Post. Since you have already generated the app -- incase of conflicts, you can skip the change.
Further, engine comes with a dummy application, located at spec/dummy because we told it to do that with the --dummy_path option. This dummy application is just a bare-bones Rails application that can be used to test the engine as if it was mounted inside a real application.
Then, you need to change rspec to use this dummy app, by making following changes:
inside spec/spec_helper.rb change this line
require File.expand_path("../../config/environment", __FILE__)
to this
require File.expand_path("../dummy/config/environment",__FILE__)
As config/environment.rb file doesn’t live two directories up, but rather inside spec/dummy.
Now, you can run the migration by following command.
RAILS_ENV=test bin/rake db:migrate
Why not db:test:prepare?
We cannot run rake db:test:prepare because it is unavailable. This
db:migrate task is especially altered for engines, and will run the migrations for the engine PLUS migrations within the spec/dummy/db folder.
Try to recreate your test DB
without rbenv
RAILS_ENV=test rake db:drop
RAILS_ENV=test rake db:create
RAILS_ENV=test rake db:test:prepare
with rbenv
RAILS_ENV=test bundle exec rake db:drop
RAILS_ENV=test bundle exec rake db:create
RAILS_ENV=test bundle exec rake db:test:prepare
I'm having problems with the syntax for the Clockwork scheduler process. I'm actually having similar issues to what is discussed in this thread but never fully answered(How do I use Rails clockwork gem to run rake tasks?)
My 'scheduler.rake' is working correctly when I test it with a 'heroku rake send_notifications'. My clock.rb process is working too as it will trigger every 30 seconds. However, I'm having issues with the clock.rb's syntax to correctly run the 'send_notifications' task in my 'rake.scheduler'. Here's what it looks like:
# scheduler.rake
desc "This task is called by the Heroku scheduler add-on"
task :send_notifications => :environment do
puts "this test is working correctly!"
end
Here's what clock.rb looks like:
require File.expand_path('../../config/boot', __FILE__)
require File.expand_path('../../config/environment', __FILE__)
require 'clockwork'
include Clockwork
every(30.seconds, 'Send notifications') {
# Heroku::API.new.post_ps('pocket-pal', 'rake scheduler:send_notifications')
rake scheduler:send_notifications
}
As you can see I've tried with a detached process using the Heroku API and invoking rake.
When I use the Heroku API, I get this error:
ERROR -- : uninitialized constant Heroku (NameError)
When I invoke rake, I get the following error:
ERROR -- : undefined local variable or method `send_notifications' for main:Object (NameError)
Does anyone know what the correct syntax is for either approach?
This answer works, however you need to follow its string syntax exactly. So in your case:
every(30.seconds, 'Send Notifications') {
`rake scheduler:send_notifications`
}
That means making sure to use ` ` for wrapping the rake task, not " " as that's tripped a few people up.
The docs from Hereko on implementing clockwork might help
Scheduled Jobs and Custom Clock Processes in Ruby with Clockwork
When I try to run my spec, I get an uninitialized constant error. My spec looks like this:
describe Facility do
it { should have_many(:units) }
it { should have_many(:facilities_users) }
it { should have_many(:administrators) }
it { should have_many(:facility_employees) }
end
The error is:
facility_spec.rb:1:in `<top (required)>': uninitialized constant Facility (NameError)
I certainly have a Facility model, so I'm not sure why this would happen.
You should try running rake spec instead of rspec spec.
But both may work.
If not working try Try bundle exec rspec spec or bundle exec rake spec.
Source: When trying to run rspec I get uninitialized constant.
Add the following at the top of your file:
require 'spec_helper'
If you are using the 'rspec-rails' gem, then run
rails g rspec:install
This will create the spec/spec_helper.rb file (you should edit it if you're not using ActiveRecord so it runs you spec setup correctly).
After that, ensure you are requiring the helper at the top of your spec files:
require 'spec_helper'
If this didn't work for you, there might be more issues like:
You're trying to test a file under the lib/ directory. In this case,
make sure this file is loaded with the environment
(config/application.rb -> autoload_paths) or require it explicitly.
The constant actually doesn't exist. It could be inside a namespace or just a typo.
In the spec file, require the file where Facility class is defined.
My hair is all gone and I'm about to throw my new macbook through the window.
I am trying to execute the famous db:migrate command and I get the following:
rake aborted!
no such file to load -- rake/dsl_definition
I am using rake 0.8.7...it's installed and included in my gemfile. I didn't have require 'rake/dsl_definition' in my rake file and I was getting the famous uninitialized-constant error. Now that I have added require 'rake/dsl_definition' to my rake file I get the error above. Anyyyyy ideas...I have searched and tried a million things with no luck... My migrations worked 2 days ago, now suddenly they don't. I have uninstalled rake 0.9.2 and also did a few other thing suggested in the link below, but nothing worked.r
How to fix the uninitialized constant Rake::DSL problem on Heroku?
heroku rake require 'rake/dsl_definition' fix not working + breaking local rake
How to fix the uninitialized constant Rake::DSL problem on Heroku?
Having rolled back your rake to 0.8.7, you need to remove the require rake/dsl-definition line from your rakefile. You only get the uninitialised constant error with later rakes.