RSpec is giving me a LoadError - ruby-on-rails

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.

Related

When switching from Minitest to RSpec in Rails, how to make `rails test` (or `rake test`) run the RSpec tests?

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’])

rspec --only-failures error NoMethodError 'fully_formatted'

I am attempting to set up rspec to run only failures with rspec --only-failures. When I do this I get this error
block in fully_formatted_failed_examples': undefined method 'fully_formatted'
for #
<RSpec::Core::Notifications::ExampleNotification:0x00007fce3766b108> (NoMethodError)
When I run be rspec --only-failures I get this error:
bundler: failed to load command: rspec (/Users/mikeheft/.rbenv/versions/2.5.0/bin/rspec)
I have config.example_status_persistence_file_path = './spec/examples.txt' set up in spec_helper.rb
When I check out the source code on GitHub, I can see the line it's erroring out on, but I don't see a way on my end to fix this as it's in the gem it looks like. I have also opened an issue on Github.

Rails ci_reporter cannot load such file

I want to use ci_reporter to generate report xml, and then let jenkins publish error message.
I added this into gemfile
gem "ci_reporter"
Put following code into Rakefile
require 'ci/reporter/rake/rspec'
task :rspec => 'ci:setup:rspec'
Created a task file and put following code in lib/ci_report.rake
namespace :ci do
task :all => ['ci:setup:rspec', 'rspec']
end
Then typed rake ci:all in terminal, but it threw this error
rake aborted!
LoadError: cannot load such file -- ci/reporter/rake/rspec
What did I miss?
update
After searching, I still felt confused. However, I found anther gem called rspec_junit_formatter. It works perfectly!

After installing minitest rails, rake test doesn't do anything

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

Why won't my rails spec run?

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.

Resources