How to write into Rspec output log file from rspec test case? - ruby-on-rails

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

Related

Can I set rspec --format documentation as the default?

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.

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.

Rspec spec failing -- undefined method `permissions' for RSpec::ExampleGroups::UserPolicy:Class (NoMethodError)

I'm installing an app with Pundit authorization and when I try to run RSpec tests I get:
undefined method `permissions'
for RSpec::ExampleGroups::UserPolicy:Class
(NoMethodError)
Also remember to add this your rails_helper
require 'pundit/rspec'
Found out what the problem was...
Following line in rails_helper.rb was commented out:
# The following line is provided for convenience purposes. It has the downside
# of increasing the boot-up time by auto-requiring all files in the support
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
Activating it made the tests work correctly :)
I had require 'pundit/rspec' but I had misspelt "policies" in the spec directory structure:
spec/polices/my_policy_spec.rb
My mistake also returned the permission error message.
You can fix it by spelling policies correctly, alternatively, set the type of the test file to policy.
RSpec.describe MyPolicy, type: :policy do
...
...
end

How to load test data from test/fixtures yml file in Rails units

How to load test data from test/fixtures yml file in Rails units
fixtures/xxxs.yml
- one:
clm1: test
clm2: test
test/unit/xxx.rb
test "load_yml_obj" do
assert_not_nil xxxs(:one)
end

Resources