Why is this Sidekiq fake test not increasing the size of the jobs array? - ruby

I have the following test setup for a sidekiq test using the fake testing in https://github.com/mperham/sidekiq/wiki/Testing.
spec_helper.rb
require 'sidekiq/testing'
Sidekiq::Testing.fake!
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
config.before(:suite) do
FactoryGirl.reload
FactoryGirl.define do
to_create { |instance| instance.save }
end
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
Airbrake.configure do |c|
c.project_id = ENV['AIRBRAKE_PROJECT_ID']
c.project_key = ENV['AIRBRAKE_PROJECT_KEY']
end
end
config.before(:each, job: true) do
Sidekiq::Worker.clear_all #make sure jobs don't linger between tests
end
config.around(:each) do |example|
DatabaseCleaner.cleaning do
example.run
end
end
config.include FactoryGirl::Syntax::Methods
end
notification_spec.rb
require 'spec_helper'
RSpec.describe NotificationWorker do
it "perform should call Airbrake#notify", job: true do
notification_worker = NotificationWorker.new
message = "This is your error message"
expect { notification_worker.perform(message) }.to change(NotificationWorker.jobs, :size).by(1)
end
end
notification_worker.rb
class NotificationWorker
include Sidekiq::Worker
sidekiq_options queue: :high
def perform(message)
Airbrake.notify(message)
end
end
Yet, why do I receive the following error message:
Failure/Error: expect { notification_worker.perform(message) }.to change(NotificationWorker.jobs, :size).by(1)
expected #size to have changed by 1, but was changed by 0
It seems as if the jobs array should be incremented by 1. What is going on? Is it a threading issue caused by the interaction between RSpec and Database Cleaner?

Because it's calling the perform method directly and not Sidekiq's API.
Try NotificationWorker.perform_async(message)

Related

Uninitialized constant NameError in Rspec

When I run rails c, I can call the following class and the method works:
test = SlackService::BoardGameNotifier
test.create_alert("test")
>>method works
I'm trying to set this up in rspec like this:
require 'spec_helper'
require 'slack-notifier'
RSpec.describe SlackService::BoardGameNotifier do
describe '#notify' do
#notifier = SlackService::BoardGameNotifier
it 'pings Slack' do
error = nil
message = "test"
expect(notifier).to receive(:ping).with(message)
notifier.send_message()
end
end
end
But I keep getting the error:
NameError:
uninitialized constant SlackService
Does this have to do with how I set up the module?
My current setup:
slack_service/board_game_notifier.rb
module SlackService
class BoardGameNotifier < BaseNotifier
WEBHOOK_URL = Rails.configuration.x.slack.url
DEFAULT_OPTIONS = {
channel: "board-games-channel",
text: "board games alert",
username: "bot",
}
def create_alert(message)
message #testing
end
end
end
slack_service/base_notifier.rb
module SlackService
class BaseNotifier
include Singleton
def initialize
webhook_url = self.class::WEBHOOK_URL
options = self.class::DEFAULT_OPTIONS
#notifier = Slack::Notifier.new(webhook_url, options)
end
def self.send_message
message = instance.create_alert("test")
instance.notify(message)
end
def notify(message)
#notifier.post blocks: message
end
end
end
Add this to your spec_helper.rb
# spec_helper.rb
ENV["RAILS_ENV"] ||= "test"
require File.expand_path("../config/environment", __dir__)
When running RSpec, Rails doesn't automatically boot up, and therefore doesn't automatically load all the libraries.
Also, I'd suggest creating a .rspec in your app's root folder with the following lines so that spec_helper is automatically loaded for all your RSpec tests:
# .rspec
--format documentation
--color
--require spec_helper
I would use the described_class from Rspec
require 'spec_helper'
require 'slack-notifier'
RSpec.describe ::SlackService::BoardGameNotifier do
describe '#notify' do
it 'pings Slack' do
error = nil
message = "test"
expect(described_class).to receive(:ping).with(message)
notifier.send_message()
end
end
end

Rspec doesn't clear factory objects after some specs

Im using clear ruby (ruby 2.3.5) without Rails and ActiveRecord and trying to write specs for some service. And if I run only part of file (including problem spec), it passed. If I run the whole file - the problem spec is failed.
By the way, I already use rspec/retry, and it settings for 20 times retry (And I see in logs, that the problem spec is failed in all 20 times).
ballot_spec.rb
# frozen_string_literal: true
require 'spec_helper'
require 'byebug'
RSpec.describe Ballot do
describe '#run' do
let(:kingdoms) { build_list(:kingdom, 6) }
let(:service) { described_class.new(kingdoms) }
before { allow(service).to receive(:hold_ballot) }
it 'holds ballot if cannot_finish_ballot? returns true' do
allow(service).to receive(:cannot_finish_ballot?).and_return(true)
service.run
expect(service).to have_received(:hold_ballot).at_least(:once)
end
it "doesn't hold ballot if cannot_finish_ballot? returns false" do
allow(service).to receive(:cannot_finish_ballot?).and_return(false)
service.run
expect(service).not_to have_received(:hold_ballot)
end
it 'returns Struct object' do
expect(service.run.class.superclass).to be Struct
end
end
describe '#hold_ballot' do
let(:all_kingdoms) { build_list(:kingdom, rand(2..10)) }
let(:pretendents) { all_kingdoms.first(rand(2..all_kingdoms.count)) }
let(:message) { build(:message, from: all_kingdoms.sample, to: all_kingdoms.sample) }
let(:expected_message_count) { pretendents.count * all_kingdoms.count }
let(:new_service) { described_class.new(pretendents) }
before do
allow(Message).to receive(:new).and_return(message)
allow(message).to receive(:send)
new_service.send('hold_ballot')
end
\/\/ THE PROBLEM IS IN THIS SPEC \/\/
it 'prepares messages to all existed kingdoms from every pretendent' do
expect(Message).to have_received(:new).exactly(expected_message_count).times
end
it 'only 6 of messages will be selected to be sent' do
expect(message).to have_received(:send).exactly(6).times
end
it 'resets Kingdoms' do
allow(Kingdom).to receive(:reset)
new_service.run
expect(Kingdom).to have_received(:reset).at_least(:once)
end
end
end
when I run rspec spec/services/ballot_spec.rb:26 (for test whole method '#hold_ballot') every spec is passed.
when I run rspec spec/services/ballot_spec.rb, I got failure:
1) Ballot#hold_ballot prepares messages to all existed kingdoms from every pretendent
Failure/Error: expect(Message).to have_received(:new).exactly(expected_message_count).times
(Message (class)).new(*(any args))
expected: 4 times with any arguments
received: 260 times with any arguments
when I use byebug, I see, that class Kingdom has dozens of objects, but variables "all_kingdoms" and "pretendents" contains not more than 10 objects.
So the once rootcause I can see here - rspec doesn't clear kingdoms, created during testing method '#run', but how to push it destroy them? I don't use ActiveRecord, don't use Rails, so cannot tun "reload!" or "destroy" explicitly. Tried to run GC.start, but it doesn't help. What can I do? Many thanks! )
spec_helper.rb
# frozen_string_literal: true
require './models/kingdom.rb'
require './models/message.rb'
require './services/message_compose.rb'
require 'factory_bot'
require 'ffaker'
require 'capybara'
require 'rspec/retry'
require './spec/factories/kingdom.rb'
require './spec/factories/message.rb'
RSpec.configure do |config|
config.verbose_retry = true
config.display_try_failure_messages = true
config.around :each do |ex|
ex.run_with_retry retry: 20 unless ex.run_with_retry
end
config.include FactoryBot::Syntax::Methods
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
config.shared_context_metadata_behavior = :apply_to_host_groups
end

RSpec why is before(:each) never executed?

I have this simple code
require 'json'
module Html
class JsonHelper
attr_accessor :path
def initialize(path)
#path = path
end
def add(data)
old = JSON.parse(File.read(path))
merged = old.merge(data)
File.write(path, merged.to_json)
end
end
end
and this spec (reduced as much as I could while still working)
require 'html/helpers/json_helper'
describe Html::JsonHelper do
let(:path) { "/test/data.json" }
subject { described_class.new(path) }
describe "#add(data)" do
before(:each) do
allow(File).to receive(:write).with(path, anything) do |path, data|
#saved_string = data
#saved_json = JSON.parse(data)
end
subject.add(new_data)
end
let(:new_data) { { oldestIndex: 100 } }
let(:old_data) { {"test" => 'testing', "old" => 50} }
def stub_old_json
allow(File).to receive(:read).with(path).and_return(#data_before.to_json)
end
context "when given data is not present" do
before(:each) do
puts "HERE"
binding.pry
#data_before = old_data
stub_old_json
end
it "adds data" do
expect(#saved_json).to include("oldestIndex" => 100)
end
it "doesn't change old data" do
expect(#saved_json).to include(old_data)
end
end
end
end
HERE never gets printed and binding.pry doesn't stop execution and tests fail with message No such file or directory # rb_sysopen - /test/data.json
This all means that before(:each) never gets executed.
Why?
How to fix it?
It does not print desired message because it fails at the first before block. Rspec doc about execution order
It fails because you provided an absolute path, so it is checking /test/data.json
Either use relative path to the test ie. ../data.json (just guessing),
or full path.
In case of rails:
Rails.root.join('path_to_folder_with_data_json', 'data.json')

Why can't RSpec find the Airbrake env keys in a test involving Sidekiq when I specify environment?

Here is my setup:
airbrake.rb
require 'airbrake'
Airbrake.configure do |c|
c.ignore_environments = [:test, :development]
c.project_id = ENV['PROJECT_ID']
c.project_key = ENV['PROJECT_KEY']
end
use Airbrake::Rack::Middleware
spec_helper.rb
RSpec.configure do |config|
config.before(:suite) do
FactoryGirl.reload
FactoryGirl.define do
to_create { |instance| instance.save }
end
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
Airbrake.configure(:test) do |c|
c.project_id = ENV['PROJECT_ID']
c.project_key = ENV['PROJECT_KEY']
end
end
config.around(:each) do |example|
DatabaseCleaner.cleaning do
example.run
end
end
config.include FactoryGirl::Syntax::Methods
end
worker_test_spec.rb
require 'spec_helper'
RSpec.describe NotificationWorker do
it "perform should call Airbrake#notify" do
anotification_worker = LNotificationWorker.new
airbrake_notification_worker.perform("some error message"))
expect(Airbrake).to receive(:notify).with("some error message")
end
end
I call Airbrake#notify in other (non-Sidekiq) tests, and they find the appropriate ENV variables just fine.
Yet if I run the above Sidekiq test with the above setup, I get the following error:
Airbrake::Error:
the 'default' notifier isn't configured
But if I change the Airbrake config in spec_helper.rb to:
Airbrake.configure do |c|
c.project_id = ENV['PROJECT_ID']
c.project_key = ENV['PROJECT_KEY']
end
the ENV keys are able to be found in the tests. Why is this?
When you say Airbrake.configure(:test), it does not mean "configure Airbrake for the test RAILS_ENV". Rather :test creates a non-default named notifier. Then you can send specific notifications to that notifier by saying Airbrake.notify("oops", {time: Time.now}, :test). But that is not about development/test/production, it is about categorizing your notifications.
So the problem is that you have configured a notifier named test, but you have not yet configured one named default, and default is what Airbrake wants to use when you don't tell it otherwise. That's why your spec passes when you say simply Airbrake.configure { ... }.

RSpec: Always execute before(:all) in begin/rescue

I'm writing Selenium tests, using Watir-Webdriver and RSpec, which can be a bit spotty when they're first being developed. I've run into a situation where I want to create something on the UI in before :all, however it can throw exceptions (based on timing or poor loading). When that happens I want to take a screenshot.
Here's what I have:
RSpec.configure do |config|
config.before(:all) do |group| #ExampleGroup
#browser = Watir::Browser.new $BROWSER
begin
yield #Fails on yield, there is no block
rescue StandardError => e
Utilities.create_screenshot(#browser)
raise(e)
end
end
end
I run it and get an error:
LocalJumpError: no block given (yield)
The reason I assumed yielding would work is RSpec's definition of before:
def before(*args, &block)
hooks.register :append, :before, *args, &block
end
How can I wrap the code I've put in my before :all in a begin/rescue block without having to do it on every suite?
Thanks in advanced.
The code you've written in the before hook is the &block you're referring to in RSpec::Hooks#before. The hook yields to your code, then runs your tests after the yield is complete.
As for how to make this work, I think this should do:
RSpec.configure do |config|
# ensures tests are run in order
config.order = 'defined'
# initiates Watir::Browser before all tests
config.before(:all) do
#browser = Watir::Browser.new $BROWSER
end
# executes Utilities.create_screenshot if an exception is raised by RSpec
# and the test is tagged with the :first metadata
config.around(:each) do |example|
example.run
Utilities.create_screenshot(#browser) if example.exception && example.metadata[:first]
end
end
This configuration requires the first test be tagged with metadata:
describe Thing, :first do
it "does something" do
# ...
end
end
This way, you'll only take a screenshot at the beginning of your run for a failing test, and not after every failing test. If you'd rather not mess with metadata (or prefer your tests are run in random order), you could do something like this:
RSpec.configure do |config|
# initiates Watir::Browser before all tests
config.before(:all) do
#test_count = 0
#browser = Watir::Browser.new $BROWSER
end
# executes Utilities.create_screenshot if an exception is raised by RSpec
# and the test is the first to run
config.around(:each) do |example|
#test_count += 1
example.run
Utilities.create_screenshot(#browser) if example.exception && #test_count == 1
end
end
This works for me. Instead of begin/rescue in the before :all hook,
config.after :each do
example_exceptions = []
RSpec.world.example_groups.each do |example_group|
example_group.examples.each do |example|
example_exceptions << !example.exception.nil?
end
end
has_exceptions = example_exceptions.any? {|exception| exception}
#Handle if anything has exceptions
end

Resources