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.
Related
I've recently been trying to run RSpec in a Rails application and I can't seem to get it to work. I've followed all the instructions and I still can't even get a simple test to run.
RSpec is in my Gemfile, and I've run rails generate rspec:install.
I have the spec folder set up with a simple model test. However, every time I try to run RSpec, I get the following error:
An error occurred while loading ./spec/models/user_spec.rb. - Did you mean?
rspec ./spec/spec_helper.rb
Failure/Error: require File.expand_path('../config/environment', __dir__)
LoadError:
cannot load such file -- rexml/document
My user_spec.rb looks like this:
require 'rails_helper'
RSpec.describe User, type: :model do
it "tests my rspec installation"
end
I'm new to Rails so I would really appreciate any help!
Just add gem 'rexml' to your Gemfile and run bundle in terminal.
When adding RSpec to Rails, we can finally run rails spec (or bundle exec rspec) to run the RSpec tests.
However, rails test confusingly still attempts to run the (non-existent) Minitest tests.
How can the "test" rake task be configured to run the RSpec tests instead?
You can override the test task like this:
# Add the code below in the Rakefile
require File.expand_path(‘config/application’, __dir__)
Rails.application.load_tasks
Rake::Task['test'].clear
task :test do
Rake::Task['rspec’].invoke
end
Another way (I only tried in my console with a task that puts something but it should work with rspec):
# Add the code below in the Rakefile
require File.expand_path(‘config/application’, __dir__)
Rails.application.load_tasks
task(:test).clear.enhance([‘rspec’])
I'm missing something, just not sure what. Sidekiq is up and running fine, I can see it in my terminal.
I have this worker, defined in app/workers/sqs_worker.rb
class SqsWorker
include Sidekiq::Worker
require 'aws-sdk'
def perform
#do something
end
end
And then in just a test file at app/test.rb I have the very simple code:
require 'sidekiq'
SqsWorker.new.perform_async
When I run the test.rb file I get this error: uninitialized constant SqsWorker (NameError)
Where did I go astray? I'm running Sidekiq (4.1.4)
I tried killing the running processes and restarting both Sidekiq and Redis to no luck.
uninitialized constant SqsWorker (NameError) indicates that your script in test.rb is not able to locate class SqsWorker
All you need to do is replace require 'sidekiq' with require_relative 'workers/sqs_worker' to make your script aware about location of SqsWorker class.
Probably, you ran the test.rb from outside of the scope of the application with something like that:
ruby app/test.rb
But for this purpose, You need to add to your test something like this:
require 'rubygems'
require 'bundler/setup'
require File.expand_path('../config/environment', __FILE__)
SqsWorker.new.perform_async
And run as this:
bundle exec ruby app/test.rb
Why do you need this? Because nowadays, the bundler manages your dependencies added to your app and therefore, you need to load the rails environment too and the last will load all the things under app/ basically.
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.
The question title pretty much sums it up, but here's a more chronological description:
I started a new rails 3.2.9 app, did not pass any special options (ie. did not skip test unit).
I added minitest-rails to the gemfile and ran bundle install.
I deleted the contents of the test folder, and ran rails g mini_test:install.
Now if I run rake test, nothing happens.
I can make my own rakefile and specify TestTask manually, but I don't get the options to do things like rake test:controllers that are supposed to come built-in unless I manually dupe all that.
Has anyone else run into this?
Make sure you add require 'test_helper' on top of your test file. e.g.
require 'test_helper'
class UsersControllerTest < ActionController::TestCase
test "should pass" do
assert true
end
end
The auto generated test_helper file I have looks like that:
ENV["RAILS_ENV"] ||= "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
class ActiveSupport::TestCase
ActiveRecord::Migration.check_pending!
fixtures :all
end
Glad you are making the switch to MiniTest! I may be able to help you get on the right track.
Honestly, I would avoid rake entirely. Try running a test from the command line to make sure your testing suite is working.
ruby -Itest test/unit/something.rb
After you know your tests are passing then get guard-minitest and set it up to watch your files. When you save a change it will automatically run the test for you. The worst part of minitest and guard is the set up but once you get it going right you'll never want to go back.
https://github.com/guard/guard-minitest
Cheers
I guess you had not run/generate any controller or scaffold command so far.
Once you create a scaffold / controller / model and migrate the database your rake test will start working
Regarding rake test:controllers, when I tried to list out with rake -T it is not still listing
You may need to register minitest-rails as the default testing engine by adding the following to your config/application.rb file:
config.generators do |g|
g.test_framework :mini_test
end
After that, you can run controller tests with the following:
rake minitest:controllers