Rails/RSpec toggle cache for a single test - ruby-on-rails

So in my app I can disable the cache for all tests, which would be ideal, but apparently there are a number of legacy tests that rely on the cache being functional. Is there a way to enable the Rails cache for a single RSpec test?
Something like:
before(:each) do
#cache_setting = Rails.cache.null_cache
Rails.cache.null_cache = true
end
after(:each) do
Rails.cache.null_cache = #cache_setting
end
it 'does not hit the cache' do
...
end

in spec_helper.rb
RSpec.configure do |config|
config.before(:example, disable_cache: true) do
allow(Rails).to receive(:cache).and_return(ActiveSupport::Cache::NullStore.new)
end
config.after(:example, disable_cache: true) do
allow(Rails).to receive(:cache).and_call_original
end
end
in xxx_spec.rb
RSpec.describe "a group without matching metadata" do
it "does not run the hook" do
puts Rails.cache.class
end
it "runs the hook for a single example with matching metadata", disable_cache: true do
puts Rails.cache.class
end
end
https://www.relishapp.com/rspec/rspec-core/docs/hooks/filters

Related

Capybara and Factorybot - created data does not appear

I am facing a wired issue, factoryBot is creating a record in Db, but when capybara try to access it, there is no record in HTML.
I tried debugging with "byebug", on prompt, when I say
#topics => It gives me Nil data. (#topic is instance variable in topics_controller.rb -> index method )
If I do "Topic.all.first" it will show me correct record of Topic with an expected random name that is -> "Toys & Garden"
If I do "random_topic.name" -> "Toys & Garden"
I have somewhat similar setup in other feature i.e "account creation feature", it is working fine in there. Any pointer or help would be highly appreciated.
My factory file is
FactoryBot.define do
factory :topic do
name { Faker::Commerce.department(2, true) }
archived false
end
end
My Feature spec file looks like below
require 'rails_helper'
describe "topics" do
let(:user) {create(:user)}
before do
sign_user_in(user) #support fuction to sign user in
end
it "allows topics to be edited" do
random_topic = create(:topic)
visit topics_path # /topics/
expect(page).to have_text random_topic.name # Error1
click_edit_topic_button random_topic.name # Another support fuction
random_topic_name_2 = Faker::Commerce.department(2, true)
fill_in "Name", with: random_topic_name_2
check "Archived"
click_button "Update Topic"
expect(page).to have_text "Topic updated!"
expect(page).to have_text random_topic_name_2
end
end
I get the error on line marked as "Error 1" , please note that "Toys & Garden" is sample name generated by Faker Gem.
Failure/Error: expect(page).to have_text random_topic.name
expected #has_text?("Toys & Garden") to return true, got false
my Rails helper(rails_helper.rb) file setup is as below.
# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
require 'database_cleaner'
require 'capybara/rspec'
require 'shoulda/matchers'
require 'email_spec'
require "email_spec/rspec"
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
ActiveRecord::Migration.maintain_test_schema!
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end
# This is for setting up Capybara right host.
# https://stackoverflow.com/questions/6536503/capybara-with-subdomains-default-host
def set_host (host)
default_url_options[:host] = host
Capybara.app_host = "http://" + host
end
RSpec.configure do |config|
config.include Capybara::DSL
config.include Rails.application.routes.url_helpers
config.include FactoryBot::Syntax::Methods
config.include EmailSpec::Helpers
config.include EmailSpec::Matchers
config.order = "random"
config.use_transactional_fixtures = false
config.before(:each) do
set_host "lvh.me:3000"
end
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, :js => true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
My Topics Controller file is something like below
class TopicsController < ApplicationController
layout 'dashboard'
before_action :authenticate_user!
def index
#topics = Topic.all
end
end
Updated with #smallbutton.com comments, but issue continues.
SOLVED
I am using apartment gem and hence topic was getting created in public schema while test was looking into a respective tenant.
As per #thomas suggestion, I modified the code:
before do
set_subdomain(account.subdomain)
sign_user_in(user) #support fuction to sign user in
Apartment::Tenant.switch!(account.subdomain)
end
When this happens it's generally caused by one of a few things
The record isn't actually being created
From your code it doesn't appear to be that
The record is being created in one transaction while the app is running in a different transaction.
You appear to have database_cleaner configured correctly, and this shouldn't be an issue in your case, however you should be using - https://github.com/DatabaseCleaner/database_cleaner#rspec-with-capybara-example - rather than the configuration you have (which depends on the :js metadata rather than checking which driver is actually being used)
The user has configured Capybara to hit a different server than the one the test is actually running on.
Here we appear to have an issue, since you're configuring Capybara.app_host to be 'lvh.me:3000'. Port 3000 is what your dev app is generally run on, while Capybara (by default) starts your app on a random port for tests. This probably means your tests are actually running against your dev app/db instance which has nothing to do with the test app/db instance, and therefore the tests won't see anything you create in your tests. Remove the setting of Capybara.app_host unless you have an actual need for its setting (in which case remove the port from your app_host setting and enable Capybara.always_include_server_port)
This all being said, since you're using Rails 5.1+ database_cleaner should not be needed anymore. You should be able to remove all of database_cleaner, reenable use_transactional_fixtures, and set Capybara.server = :puma and have things work fine (still would need to fix the app_host setting)
Your record isn't persisted when you visit the page, so it's normal that it return false.
Try the following :
require 'rails_helper'
describe "topics" do
let(:user) {create(:user)}
let!(:random_topic) {create(:topic)}
before do
sign_user_in(user) #support fuction to sign user in
visit topics_path # /topics/
end
it "allows topics to be edited" do
expect(page).to have_text random_topic.name # Error1
click_edit_topic_button random_topic.name # Another support fuction
random_topic_name_2 = Faker::Commerce.department(2, true)
fill_in "Name", with: random_topic_name_2
check "Archived"
click_button "Update Topic"
expect(page).to have_text "Topic updated!"
expect(page).to have_text random_topic_name_2
end
end
Note that random_topic is extracted in a let! (more info about the difference between let and let! : https://relishapp.com/rspec/rspec-core/v/2-5/docs/helper-methods/let-and-let !
You should create the record before you visit the page. Currently you do it the other way around so your record cannot appear.
People having this kind of issue should definitly try this:
instance.reload after clicking and before using "expect".
This solved my issue I've been stuck on for a whole day ...

Rails 4 rspec 3 controller test: session helper module not working for before(:all), works for before(:each)

I'm building a toy chat application using Rails 4.2.7, and am writing specs for my controllers using rspec 3.5. My Api::ChatroomsController requires a user to be logged in in order to create a chatroom, so I have created a Api::SessionsHelper module to create sessions from within the Api::ChatroomsController spec.
# app/helpers/api/sessions_helper.rb
module Api::SessionsHelper
def current_user
User.find_by_session_token(session[:session_token])
end
def create_session(user)
session[:session_token] = user.reset_session_token!
end
def destroy_session(user)
current_user.try(:reset_session_token!)
session[:session_token] = nil
end
end
# spec/controllers/api/chatrooms_controller_spec.rb
require 'rails_helper'
include Api::SessionsHelper
RSpec.describe Api::ChatroomsController, type: :controller do
before(:all) do
DatabaseCleaner.clean
User.create!({username: "test_user", password: "asdfasdf"})
end
user = User.find_by_username("test_user")
context "with valid params" do
done = false
# doesn't work if using a before(:all) hook
before(:each) do
until done do
create_session(user)
post :create, chatroom: { name: "chatroom 1" }
done = true
end
end
let(:chatroom) { Chatroom.find_by({name: "chatroom 1"}) }
let(:chatroom_member) { ChatroomMember.find_by({user_id: user.id, chatroom_id: chatroom.id}) }
it "responds with a successful status code" do
expect(response).to have_http_status(200)
end
it "creates a chatroom in the database" do
expect(chatroom).not_to eq(nil)
end
it "adds the chatroom creator to the ChatroomMember table" do
expect(chatroom_member).not_to eq(nil)
end
end
end
I'm using a before(:each) hook with a boolean variable done to achieve the behavior of a before(:all) hook for creating a single session.
If I use a before(:all) hook, I get the error:
NoMethodError: undefined method `session' for nil:NilClass`
I put a debugger in the create_session method of the Api::SessionsHelper module to check self.class and in both cases, when I use before(:each) and when I use before(:all), the class is:
RSpec::ExampleGroups::ApiChatroomsController::WithValidParams
However when using the before(:each) hook, session is {}, while in the before(:all) hook, session gives the NoMethodError above.
Anybody know what causes this error?
You need to include the helper in the test block:
RSpec.describe Api::ChatroomsController, type: :controller do
include Api::SessionsHelper
end
You can also avoid duplication by including common spec helpers in spec/rails_helper.rb
RSpec.configure do |config|
# ...
config.include Api::SessionsHelper, type: :controller
end
This is also where you should put the database_cleaner config. You should use to clean between every spec not just before all as that will lead to test ordering issues and flapping tests.
require 'capybara/rspec'
#...
RSpec.configure do |config|
config.include Api::SessionsHelper, type: :controller
config.use_transactional_fixtures = false
config.before(:suite) do
if config.use_transactional_fixtures?
raise(<<-MSG)
Delete line `config.use_transactional_fixtures = true` from rails_helper.rb
(or set it to false) to prevent uncommitted transactions being used in
JavaScript-dependent specs.
During testing, the app-under-test that the browser driver connects to
uses a different database connection to the database connection used by
the spec. The app's database connection would not be able to access
uncommitted transaction data setup over the spec's database connection.
MSG
end
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, type: :feature) do
# :rack_test driver's Rack app under test shares database connection
# with the specs, so continue to use transaction strategy for speed.
driver_shares_db_connection_with_specs = Capybara.current_driver == :rack_test
if !driver_shares_db_connection_with_specs
# Driver is probably for an external browser with an app
# under test that does *not* share a database connection with the
# specs, so use truncation strategy.
DatabaseCleaner.strategy = :truncation
end
end
config.before(:each) do
DatabaseCleaner.start
end
config.append_after(:each) do
DatabaseCleaner.clean
end
end

Rspec and database_cleaner only remove the data added by the tests

I want to be able to always have access to my seed data on my test database.
I understand database_cleaner will remove everything if it's set up that way.
I try to remove everything and then reloading the seed, but when I try to use js: true on a test, the seed never gets loaded so i get errors saying data does not exist.
My spec_helper.rb
RSpec.configure do |config|
# before the entire test suite runs, clear the test database out completely
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
# sets the default database cleaning strategy to be transactions (very fast)
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
# For these types of tests, transactions won’t work. We must use truncation
config.before(:each, js: true) do
DatabaseCleaner.strategy = :truncation
end
# hook up database_cleaner around the beginning and end of each test, telling it to execute whatever cleanup strategy we selected beforehand.
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
# reload the seed so we have data to play with
end
config.before :all do
Rails.application.load_seed
end
end
When in my view_spec I have something like this
require 'spec_helper'
describe 'my/path', type: :view do
before do
#user = create(:user)
#user.permissions << Permission.first
login_as(#user)
visit my_path
end
it 'should have a valid user, just for kicks' do
#user.should be_valid
end
it 'should be in the path i said' do
expect(current_path).to eq(my_path)
end
describe 'click submit button', js: true do
it 'should take me to a different path' do
click_link('button_1')
expect(current_path).to eq(my_new_path)
end
end
end
The first two test will run and be ok with creating that user, but as soon as it hits that last test with js: true, it no longer has Permission in the database.
Is there a way to tell database_cleaner to only delete the data added by rspec? and not the seed?
Or maybe even tell it to not delete certain tables?
Any help would be appreciated.
Try to use :truncation for all tests with:
DatabaseCleaner.strategy = :truncation
RSpec.configure do |config|
config.before(:each) do
DatabaseCleaner.clean
Rails.application.load_seed
end
end
There also may be an issue with your seeds and not with DatabaseCleaner. You should debug your database state right in the failing test using puts statements or debugger (e.g. pry-byebug).

Execute pending job with ActiveJob in rspec

I have this code to test ActiveJob and ActionMailer with Rspec
I don't know how really execute all enqueued job
describe 'whatever' do
include ActiveJob::TestHelper
after do
clear_enqueued_jobs
end
it 'should email' do
expect(enqueued_jobs.size).to eq(1)
end
end
The proper way to test will be to check number of enqueued jobs as in your example, and then test each job separately. If you want to do integration testing you can try perform_enqueued_jobs helper:
describe 'whatever' do
include ActiveJob::TestHelper
after do
clear_enqueued_jobs
end
it 'should email' do
perform_enqueued_jobs do
SomeClass.some_action
end
end
end
See ActiveJob::TestHelper docs
Here is how I solved a similar problem:
# rails_helper.rb
RSpec.configure do |config|
config.before :example, perform_enqueued: true do
#old_perform_enqueued_jobs = ActiveJob::Base.queue_adapter.perform_enqueued_jobs
#old_perform_enqueued_at_jobs = ActiveJob::Base.queue_adapter.perform_enqueued_at_jobs
ActiveJob::Base.queue_adapter.perform_enqueued_jobs = true
ActiveJob::Base.queue_adapter.perform_enqueued_at_jobs = true
end
config.after :example, perform_enqueued: true do
ActiveJob::Base.queue_adapter.perform_enqueued_jobs = #old_perform_enqueued_jobs
ActiveJob::Base.queue_adapter.perform_enqueued_at_jobs = #old_perform_enqueued_at_jobs
end
end
Then in specs we can use:
it "should perform immediately", perform_enqueued: true do
SomeJob.perform_later
end
Just combined all the best pieces, +included sidekiq:
spec/support/perform_jobs.rb:
require 'sidekiq/testing'
RSpec.configure do |config|
Sidekiq::Testing.fake!
config.around(:each, perform_jobs: true) do |example|
Sidekiq::Testing.inline! do
queue_adapter = ActiveJob::Base.queue_adapter
old_perform_enqueued_jobs = queue_adapter.perform_enqueued_jobs
old_perform_enqueued_at_jobs = queue_adapter.perform_enqueued_at_jobs
queue_adapter.perform_enqueued_jobs = true
queue_adapter.perform_enqueued_at_jobs = true
example.run
ensure
queue_adapter.perform_enqueued_jobs = old_perform_enqueued_jobs
queue_adapter.perform_enqueued_at_jobs = old_perform_enqueued_at_jobs
end
end
end
spec/some_spec.rb:
it 'works', perform_jobs: true do
...
end
I have an :inline_jobs helper, where applied to a test it will perform the enqueued jobs and clear the enqueued jobs
module InlineJobHelpers
def self.included(example_group)
example_group.around(:each, :inline_jobs) do |example|
perform_enqueued_jobs do
example.run
end
ensure
clear_enqueued_jobs
end
end
end
RSpec.configure do |config|
config.include ActiveJob::TestHelper, :inline_jobs
config.include InlineJobHelpers, :inline_jobs
end

Simulating a non-local request in Rails/RSpec request testing

I'd like to block off access to the application to all non-local requesters (my application's actual functionality in practice is more sophisticated, but figuring out how to do this will solve my specific issue). How would I go about testing this with request tests in RSpec?
In spec/requests/gatekeeper_spec.rb
describe "A local request to the site root" do
before :each do
get root_path
end
it "should not allow access" do
response.status.should be(401)
end
end
describe "An external (terminology?) request to the site root" do
before :all do
# TODO: make request remote
end
before :each do
get root_path
end
it "should allow access" do
response.status.should be(200)
end
end
How should I implement the # TODO line? I've looked into mocks and think that rigging request.remote_ip may be appropriate, but I'm not certain exactly how such a mock is implemented.
If I understand correctly, test requests have a remote address of "0.0.0.0", so they would normally be considered remote and you'd want to stub the local requests, not the other way around.
I think this should work for controller specs -- not sure about request specs:
request.stub(:local?) { true }
Untested, but should work in Rails 2.3.x and 3.0:
before :each do
Rails::Initializer.run do |config|
config.action_controller.consider_all_requests_local = false
end
end
after :each do
Rails::Initializer.run do |config|
config.action_controller.consider_all_requests_local = true
end
end
In Rails 4 you can do it with:
RSpec.configure do |config|
config.before(:each, allow_rescue: true) do
Rails.application.config.action_dispatch.stub(:show_exceptions) { true }
Rails.application.config.stub(:consider_all_requests_local) { false }
end
end
And then in your test file:
describe "A test" do
it "renders custom error pages", :allow_rescue => true do
# ...
end
end
The name :allow_rescue is taken from a ActionController::Base.allow_rescue configuration which exists in Rails 3, and there the RSpec configuration would be:
RSpec.configure do |config|
config.before(:each, allow_rescue: true) do
ActionController::Base.stub(:allow_rescue) { true }
end
end

Resources