Where should i place helper method for rspec test in Rails6? - ruby-on-rails

I have helper method for rspec test like this :
<bla_bla_helper.rb>
Module blabla
def bla
end
end
Where should i place this module?
This module is only for rspec test.
Past version of Rails used spec/support directory, should i use this directory?

In RSpec you use spec/support to place the helpers, matchers, shared contexts etc that your specs need. This path is added to the load path so you can simply do require 'foo' in your specs to require spec/support/foo.rb.
Some projects use a glob in spec/spec_helper.rb to require all the files in the spec/support directory.
This is just an RSpec convention that has nothing to do with Rails, so the practice isn't going to change with Rails versions.

I usually place mine inside spec/support/helpers and include it in the configuration via rails_helper.
Here's an example of including the devise API auth helper that I am using for my application.
config.include DeviseApiAuthHelper, type: :controller

Yes you can continue using spec/support directory.

Related

How to create cucumber (capybara?) helpers

I a have some code I'd like to refactor out of my step definitions and put them inside.. helpers?
Oh and please also say how to include them, I am really having a hard time finding any solid info on that.
Straight from the rspec documentation here: https://www.relishapp.com/rspec/rspec-core/docs/helper-methods/define-helper-methods-in-a-module#include-a-module-in-all-example-groups
Include a module in all example groups
Given a file named "include_module_spec.rb" with:
require './helpers'
RSpec.configure do |c|
c.include Helpers
end
RSpec.describe "an example group" do
it "has access to the helper methods defined in the module" do
expect(help).to be(:available)
end
end
When
I run rspec include_module_spec.rb
Then
the examples should all pass
You may also benefit from a a support/helpers folder Or equivalent which is covered pretty well here: How to include Rails Helpers on RSpec

Rails 3, app/lib not loaded with rspec

I have a controller "A" which requires a file in a subdirectory of app/lib (ex: app/lib/a_folder/my_class.rb).
So I did something like this :
require 'a_folder/my_class'
class AController < ApplicationController
# Some stuff using MyClass
end
It works well when I use the application, but doesn't work when I launch RSpec.
This is how my RSpec file looks:
require 'spec_helper'
require 'lib/import_functions' # Rails.root/spec/lib/import_functions.rb
RSpec.describe AController, type: controller do
# My tests routines
end
When I start rspec it tells me the require in the controller file doesn't found the file (`require': cannot load such file), while it works well when I start the app.
I've added a puts $LOAD_PATH just before the require and it appears that Rails.root/app/lib is not present.
I use Rails 3.2 and rspec-rails 3.2.
Does anyone have any idea why it happens and how to fix it please ?
Thank you for your future answers.
lib files are not auto loaded. You can put the following configuration in your application.rb, it has a problem also it will load all files under lib directory.
config.autoload_paths += "#{Rails.root}/lib/"
Or you can load your lib files in your RSpec as following code
require_relative "../../lib/a_folder/my_class.rb"
or
require 'lib/a_folder/my_class.rb'

Before and After hooks for a spec directory in RSpec

We have quite a bit of tests in our RSpec test suite. directory structure looks something like -
spec/
truncation/
example1_spec.rb
example2_spec.rb
...
transaction/
example1_spec.rb
example2_spec.rb
...
I wanted to restore a test database dump, before all of the spec files in transaction/ folder are run and empty it after all tests finish.
Is there a way to do this?
There are before(:suite) and after(:suite) hooks but these work for individual spec files.
Is there a way to provide before and after hooks for directories in RSpec?
Are you using RSpec 3+?
You can use #define_derived_metadata to add custom metadata based on file path matchers.
RSpec.configure do |config|
config.define_derived_metadata(file_path: %r{spec/truncation}) do |metadata|
metadata[:truncation] = true
end
config.before(:all, :truncation) do
# truncate that bad boy
end
end
This is the same method used in rspec-rails to add custom behavior to specs in the specific directories, e.g. spec/controllers.
Docs

Streamline Rspec Test of Rails Helper

I am currently testing a Rails(4.2) helper with Rspec(3) successfully. However, the test file setup is a bit cumbersome. How can I streamline the require and/or include lines?
# spec/helpers/nav_helper_spec.rb
require 'spec_helper'
require_relative '../../app/helpers/nav_helper' # this seems bulky
describe NavHelper do
include NavHelper # this seems repetitive
...
end
Thanks in advance!
If you have a "default" setup you probably have a rails_helper in addition to your spec_helper. If you don't mind loading all of the Rails directories in this one spec (a bit of a performance hit) you can require that instead of the spec_helper (cleaning up the requires). But there's nothing wrong with including only what you need, it will run faster.
Rspec will also mix in the helper for you if it knows the spec type. You can either include config.infer_spec_type_from_file_location! in your spec helper, or include the type in the describe declaration:
describe NavHelper, type: :helper do
Either way you'll be able to use something like expect(helper.method_name).to eq(result) without explicitly including the module.

How to test a helper within a gem with rspec?

I already published my first rails gem but not yet with any tests included. This gem is just a simple helper which I want to test with rspec. But I have no idea how I can test a helper within a gem with rspec.
I tried to create a file spec/helper/development_ribbon_helper_spec.rb:
require 'spec_helper'
describe DevelopmentRibbonHelper do
end
But when I execute rspec I get this error:
development_ribbon_helper_spec.rb:3:in `<top (required)>': uninitialized constant DevelopmentRibbonHelper (NameError)
You can find the whole source code on github.
It looks like you may be trying to write a rails engine gem, correct? Since your helper file is located outside of lib/, your spec_helper will need to know how to load your rails engine.
One way to do this is to create a dummy rails app in your spec/ directory. This setup is well-described in this post: http://reinteractive.net/posts/2-start-your-engines.
Alternatively, you can move the helper file into lib/development_ribbon and require it in your development_ribbon.rb file:
require "development_ribbon/development_ribbon_helper"
Then, in your rails apps that use the gem, you could include the helper in application_helper.rb
module ApplicationHelper
include DevelopmentRibbon::DevelopmentRibbonHelper
end

Resources