How can I configure RSpec to run my model tests? - ruby-on-rails

No matter what I do, the only tests that run with the rake test command are those in spec/requests. Naturally, I would like to run everything in the spec directory.
I thought getting the gem and installing RSpec would do it, but it seems with these testing libraries that the whole "convention over configuration" thing is turned on its head. There's a hell of a lot of configuration.
I simply want to run all of my tests. How can I do that?

What does your Rakefile look like? You may need to add the following:
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new
Also, check that your spec's filenames all end in _spec.rb.

Just this will do:
bundle exec rspec spec
This must be fired from root directory of application. Here spec is the directory.

Related

How to disable testing of rswag files with rspec, but only use them for documentation?

I have problem with rswag spec/integration/*_spec.rb files witch goes in the main thread when I run rspec. In my project I use rswag lib only for creating documentation but not testing.
Result: rspec - run all tests except rswag. And rake rswag run only spec/integration/* for documentation.
The quickest solution I could figure out was running tests under a specific folder. In my case I have integration tests under spec/ folder and rswag tests under spec/requests/, so I can do it like this: rspec --pattern spec/*_spec.rb
In your case should be rspec --pattern spec/integration/*_spec.rb.
See more about the --pattern option here.

How do I run debugger with the Rails run Spec extension?

I am using VsCode to write and edit some rspec tests.
What I would like to be able to do is run a specific 'it' or 'describe' block in debug mode.
At this time I can run the rspec/spec file in debug mode but it executes all of the tests.
I have install 'Rails run Spec' extension which allows me to execute a specific 'it' or 'describe' block without the ability to debug.
Ideally I would like both options married together.
I have done some digging but not able to find anything that fits my scenario.
Any help would be greatly appreciated.
Joe
Add pry-byebug to your gemfile and run bundle install
# Gemfile
gem 'pry-byebug'
Then, whenever you want to inspect a test, add binding.pry inside the test.
# some_spec.rb
it "is not behaving how I want it to" do
binding.pry
expect(my_var).to eq(some_val)
end

Error when trying to run rspec: `require': cannot load such file -- rails_helper (LoadError)

I am trying to run rspec for Ruby on Rails. I am running Rails 4.1.1. I have installed the gem, have established a spec folder with some tests. I have created a directory through $ rails g rspec:install
I tried to create a testing database through $ rake db:test:prepare but it throws this error message:
WARNING: db:test:prepare is deprecated. The Rails test helper now maintains your test
schema automatically, see the release notes for details.
So I ended up looking at this stack overflow post, and of the two options, the one that worked was:
rake db:schema:load RAILS_ENV=test
So, now I need to run rspec.
When I run $ rspec spec from the command line I get this error:
/Users/myname/.rbenv/versions/2.1.1/lib/ruby/2.1.0/rubygems/core_ext/
kernel_require.rb:55:in `require': cannot load such file -- rails_helper (LoadError)
How do I resolve this so that I can start running tests?
There is some problem with rspec V3. But in your case you are using V2.
change
require 'rails_helper'
to
require 'spec_helper'
Other description find here https://teamtreehouse.com/forum/problem-with-rspec
For V3 :
If someone using rspec V3 then similar error occurs when generator not run. So before trying anything run generator.
rails generate rspec:install
If you are getting a huge list of warning on your console. Then you need to remove --warnings from .rspec file.
I actually just had this error on rails 4 with rspec 3, but in my case I forgot to run the generator:
rails generate rspec:install
Also I had to remove warnings from .rspec, as stated by one of rpsec developers
For me, the problem was due to a different "rspec-support" version loaded and activated. I solved by prepending a "bundle exec":
bundle exec rspec
Always remember to run the
rspec or the bundle exec rspec from the root of your project.
Something like:
bundle exec rspec spec/features/file_test_spec.rb
For newer versions (3.5), if you need to run the generator, rails generate rspec:install doesn't work for me. I used:
rspec --init
Creates .rspec and spec/spec_helper.rb.
EDIT:
Just in case, I found out that I installed rspec gem for ruby, not the one for Rails, so rspec:install wasn't available. Should use https://github.com/rspec/rspec-rails.
Sometimes you need to run rspec command from Project's parent directory
Please install the following gem:
gem install 'rspec-rails'
And then run:
rails generate rspec:install
This will generate the spec folder and rails_helper.rb and spec_helper.rb. Your error should be taken care once this is done.
None of these suggestions worked for me on Rails 4.2.0 with rspec-rails 3.2.3.
Instead, I placed the following two lines at the top of my spec file:
require 'rails_helper'
require 'spec_helper'
Now I can execute rspec spec/features/my_spec.rb.
I had the same error but there was no reference to rails_helper anywhere and we weren't even using rails.
However, I eventually found that my ~/.rspec was requiring rails_helper (presumably from creating rails app in the past) which was causing the issue ...
$ cat ~/.rspec
--color
--format documentation
--require spec_helper
--require rails_helper
The fix is as simple as removing that line. Hope this helps someone else grappling with the same issue!
One possibility that worked for me is to make sure you're requiring spec_helper. RubyMine can default require rspec and this can sometimes lead to this error.
You may also have your file named incorrectly. Make sure it ends in _spec.rb. I had misspelled my file to end in _soec.rb and was scratching my head for a while...
I got the error you described when running a controller test. However, when I ran a model test, I got the true error. it ended up saying that the error was occuring because my database wasn't migrated. I fixed the error by running this command...
rake db:test:prepare
For someone. If you got that error when you trynna test a specific file. you might can like this
$ bundle exec rspec ./spec/features/user_login_spec.rb
$ bundle exec rspec ./spec/models/user_login_spec.rb

How to run tests on a mountable engine

I just created a mountable engine. The test directory looks somewhat like this:
engine_name
-test
- dummy
- test_helper.rb
- models
-engine_name
-- user_test.rb
etc
I attempted to call 'rspec' -> didn't work. Error message stated it couldn't find the folder 'spec'
I then called 'rspec test' -> it found the directory, but didn't find the test.
Found that rspec looks for *_spec.rb files, so I renamed user_test.rb to user_spec.rb
rspec then found the file, but it said it couldn't find test_helper.rb (since that was required by user_spec.rb).
I have a feeling that this engine's test directory is not designed for rspec. What's the best way to test an engine?
Thanks!
Since you are using rspec, you should use the directory spec instead of test.
The test directory is the default for test unit.
If you run rspec and you have a spec directory it will grab the tests from there.
You need a right spec_helper.rb (test_helper.rb is for test unit). You can see the example of spec_helper.rb in the template from rspec-rails repository.
Then, you should require the spec_helper in the user_spec.rb and just run rspec in the terminal.

simplecov gem code coverage

i use minitest framework for testing and simplecov gem for code coverage. i have a problem about simplecov. my problem is lkie this:
i wrote a model test. when i run the test using rake minitest:models, test runs and coverage shows %100. But when i run tests using bundle exec rake, the code coverage of same tests are shown missing.
i make researching on net. some people also have problem like this about simplecov. but i could not find a solution for this.
i'm waiting your ideas. Thanks in advance.
Did you enabled simplecov by doing SimpleCov.start on top of your code?
This is required as the first statement i.e. before your code, otherwise you will never get it work.
Also include the SimpleCoV Adapter.
As following the post generating-code-coverage-metrics-for-a-ruby-on-rails-project-with-simplecov, define rules with SimpleCov putting conditions:
SimpleCov.start do
# rules here
end if ENV["COVERAGE"]
Then run the coverage on demand by specifying the coverage variable:
COVERAGE=true bundle exec rake spec

Resources