Can I set rspec --format documentation as the default? - ruby-on-rails

Per the Rspec documentation, by default when you run rspec you get the progress formatter (looks like this: ".....").
There is another formatting option rspec --format documentation that goes through each test one by one. My question: how can I enable --format documentation by default without having to type it in the command line every time?

Option 1
Add it to .rspec file (or create one in the project's root directory) - options added to it will be applied to every test run within current project:
# .rspec
--color
--format documentation
Option 2
Add it to RSpec.configure block:
RSpec.configure do |config|
config.formatter = :documentation
end
Option 3
Specify rspec's options globally by adding them to ~/.rspec.

RSpec.configure do |config|
config.color = true
config.formatter = :documentation
config.order = 'default'
end

You can create your own personal RSpec settings by creating a ~/.rspec file:
--color
--format documentation
The project .rspec file should only contain the minimum settings required to run the spec suite to avoid developer wars.

Related

How to write into Rspec output log file from rspec test case?

I have Rspec test cases but I want write some data from variables into Rspec output file for further processing, so how to write into output file generated by --out rspec_results.html
In your spec_helper.rb file write down this code
RSpec.configure do |config|
config.example_status_persistence_file_path = 'spec/examples.txt'
end

Can't set custom options in .rspec

I'm trying to put some custom options in my .rspec because I'd like to have this custom formatter.
This is the only content of my .rspec file:
--format Fuubar
--color
When i'm launching my tests with
rspec some_spec.rb
nothing is happening, but when I'm using
rspec some_spec.rb --format Fuubar --color spec
or putting the options in my spec_helper.rb
config.color = true
config.formatter = "Fuubar"
everything is working fine, so i think there's something I'm missing with the .rspec file.
Any idea?
Where is your .rspec file located? There is only 3 places where Rspec will look for this file:
$PROJECT_ROOT/.rspec-local
$PROJECT_ROOT/.rspec
~/.rspec
$PROJECT_ROOT is not an actual environment variable, but the full path to your project.
Please make sure your .rspec file is in one of these locations, and not inside the spec directory or something like that.
Source: https://www.relishapp.com/rspec/rspec-core/v/3-0/docs/configuration/read-command-line-configuration-options-from-files
The problem was the location of the .rspec file.
The correct structure should be
-- my_tests/
---- spec/
------ folder1/
------ folder2/
------ folder3/
------ spec_helper.rb
----- .rspec

RSpec is Freezing

I have rspec configured installed in my rails app. It was working fine (We are just experimenting with rspec so there's only 2 tests).
They were working fine. Now rspec is freezing when it's going to perform a test using database.
I just freezes. I don't even know were to start looking because there's no error in the output.
Is there a verbose or debugging mode for rspec or someone ever faced this?
I've tried -b but it freezes before can give an error.
Output: (Rspec is configured with --format documentation)
[leandro#machine:~] rspec
User
Than, that's it. It hangs. I has to reset manually the computer twice.
This is the user_spec.rb
require 'spec_helper'
describe User do
let(:user) { User.create({
username: "teste",
email: "teste#teste.com",
name: "Teste",
phone: "123456789",
password: "123456",
notes: "Teste"
}) }
subject { user }
it "is invalid without a username" do
user.username = nil
is_expected.not_to be_valid
end
end
And my spec_helper.rb
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
# Checks for pending migrations before tests are run.
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
config.color = true
config.tty = true
config.formatter = :documentation #:progress, :html, :textmate
config.expect_with :rspec do |c|
c.syntax = :expect
end
end
SOLUTION
It turns out that delayed_job_active_record gem was causing the hanging.
I don't know why, but as #Greg Malcolm I looked into the log/teste.log and rspec was feeezing right after createing the delayed jobs database and setting up a new user.
I've restricted the gem just for development and production enviroment, and it worked!
I haven't heard of a rake like verbose feature for rspec. But it's probably more useful to look through log/test.log and see what shows up there. It shows database activity which is part of a freeze effect.
You might also want to rails console into test and make sure your database connection is working right.
You have to check the ./log/test.log file. As this file tends to grow big, use the tail command to read the last n lines.
For example, to read the last 5 lines of the log file, execute the following command:
tail -5 ./log/test.log
In my case, my Google Pub/Sub emulator was hanging.
[ActiveJob] Failing with status #<struct Struct::Status code=4, details="Deadline Exceeded", metadata={}>
[ActiveJob] calling pubsub.googleapis.com:443:/google.pubsub.v1.Publisher/GetTopic
It might also be helpful to use tail -f ./log/test.log, to keep track of the logs while RSpec is running. You can read more about it on "Tail Your Test Logs", by thoughtbot.
It's pretty old question but in case anybody else will get this. I've got the same strange Rspec behaviour today because of left --drb option in project .rspec file after removing DRb server (spork or zeus). Just make sure Rspec have DRb disabled if you don't have spork or zeus installed.

Capybara::ElementNotFound on all specs only on Travis

I'm using selenium, capybara with rspec for testing, i noticed all tests pass locally, but on travis all tests fail with this error on each spec.
Capybara::ElementNotFound:
I'm not sure what is wrong here, here is the specs runner on .travis.yml
script:
- xvfb-run bundle exec rspec spec/features/*.rb
for spec_helper.rb here is my config:
RSpec.configure do |config|
config.treat_symbols_as_metadata_keys_with_true_values = true
config.use_transactional_fixtures = false
config.infer_base_class_for_anonymous_controllers = false1
config.filter_run_excluding :broken => true
config.render_views
Capybara.default_driver = :selenium
Capybara.default_selector= :css
Capybara.run_server = true
Capybara.default_wait_time = 30
Well I found the reason of the issue and the solution.
Reason: We use responsive design, and apparently travis uses a small window size which turned on the mobile design and that is why it couldn't find the elements.
Solution:
script:
- xvfb-run --server-args="-screen 0 1024x768x24" bundle exec rspec spec/features/*.rb
You basically set windows size to a larger size.

Skipping specific rspec tests in guard

I'm trying to get the guard to skip some tests (those that require selenium to run).
I've added browser_required tag to them and running rspec with "--tag ~browser_required" filters them out.
However I can't get guard not to run them, I've set :cli => "--tag ~browser_required" in guardfile. This is my full guard file http://pastebin.com/pGuWAQm8
In your gist it does have two rspec blocks (one with cli and another without). Perhaps deleting the second one will at least get this working.
Check my config: https://github.com/lucassus/locomotive/blob/master/Guardfile
You could pass cli arguments to rspec command and using --filter options you could reject some specs
guard 'rspec', :version => 2, :cli => "--drb --color --tag ~slow:true" {}
In your example you can use --tag ~js:true
The cli option is deprecated in Rspec 3. Use cmd instead.
For example:
guard :rspec, cmd: "bundle exec rspec --color --tag ~speed:slow", failed_mode: :focus do
require "guard/rspec/dsl"
dsl = Guard::RSpec::Dsl.new(self)
# RSpec files
rspec = dsl.rspec
watch(rspec.spec_helper) { rspec.spec_dir }
watch(rspec.spec_support) { rspec.spec_dir }
watch(rspec.spec_files)
watch(%r{\Aapp/(.+)\.rb\z}) { |m| "spec/#{m[1]}_spec.rb" }
end
Read more on Rspec tags here

Resources