Help me. I don't understand how to fix it. I tried to many variations...
driver_iq_driver_spec.rb
describe '.perform' do
it 'correctly parse response' do
driver = described_class.new(dot_application, background_check_type, provider_setting).perform
expect(driver).to be_instance_of(BackgroundCheck)
expect(driver).to have_attributes(status: 'inprogress', background_check_type_id: 4)
end
context 'when exception when status is Error' do
before { allow_any_instance_of(described_class).to receive(:driver_iq_api).and_return('https://test/error') }
it 'returns error message' do
expect { described_class.new(dot_application, background_check_type, provider_setting).perform }.
to raise_error(RuntimeError)
end
end
end
Error: RSpec/AnyInstance: Avoid stubbing using allow_any_instance_of.
before { allow_any_instance_of(described_class).to receive(:driver_iq_api).and_return('https://test/error') }
You have a pretty specific instance of your described_class that you can stub:
context 'when exception when status is Error' do
let(:subject) do
described_class.new(dot_application, background_check_type, provider_setting)
end
before do
allow(subject).to receive(:driver_iq_api).and_return('https://test/error')
end
it 'returns error message' do
expect { subject.perform }.to raise_error(RuntimeError)
end
end
Assuming perform raises the error, not initializing the instance.
Related
How to get the test pass for this error?
Rspec controller and result
context 'invalid confirmation_token' do
subject do
post signup_step5_path,
params: {
user: {
password: 'hoge',
password_confirmation: 'hoge',
confirmation_token: 'wrong_token'
}
}
end
let(:user) { User.find_by(confirmation_token: 'testtesttest') }
it 'does not update user attributes and never create an end_point record' do
expect { subject }.raise_error(ActiveRecord::RecordNotFound)
expected ActiveRecord::RecordNotFound but nothing was raised
controller-method
I rescue ActiveRecord::RecordNotFound and render 404 page in the private method.
class Users::SignupController < ApplicationController
layout 'devise'
rescue_from ActiveRecord::RecordNotFound, with: :render404
def step5
#user = User.find_by(confirmation_token: step5_params[:confirmation_token])
raise ActiveRecord::RecordNotFound unless #user
.....
end
private
def render404(error = nil)
logger.info "Rendering 404 with exception: #{error.message}" if error
render file: Rails.root.join('public/404.ja.html'), status: :not_found
end
end
First its probably a good idea to explain that the exception matchers will only actually match uncaught exceptions. Thats because its basically just a rescue statement and rescues the exception as it bubbles up the call stack and its intended to test that a peice of code raises an exception which its up to the consumer to catch - that is an example of testing the behavior.
Testing that code raises and rescues a exception on the other hand is testing how it does its job.
def foo
raise SomeKindOfError
end
def bar
begin
raise SomeKindOfError
rescue SomeKindOfError
puts "RSpec will never catch me!"
end
end
describe "#foo" do
it "raises an exception" do
expect { foo }.to raise_exception(SomeKindOfError)
end
end
describe "#bar" do
it "rescues the exception" do
expect { bar }.to_not raise_exception(SomeKindOfError)
end
end
When you use rescue_from its basically just syntactic sugar for using an around_action callback to rescue the given exception:
class ApplicationController
around_action :handle_errors
private
def handle_errors
begin
yield
rescue SomeKindOfError
do_something
end
end
end
While RSpec did at one point have bypass_rescue for controller specs the use of controller specs is greatly discouraged by both the Rails and RSpec teams and you're really just testing the implementation instead of the behavior.
Instead you should test what the actual controller does instead of how it does it.
context 'invalid confirmation_token' do
# explicit use of subject is a code smell
before do
post signup_step5_path,
params: {
user: {
password: 'hoge',
password_confirmation: 'hoge',
confirmation_token: 'wrong_token'
}
}
end
let(:user) { User.find_by(confirmation_token: 'testtesttest') }
it 'does not update the users password' do
expect(user.valid_password?('hoge')).to be_falsy
end
it 'returns a 404 - NOT FOUND' do
expect(response).to have_http_status(:not_found)
end
# using Capybara in a feature spec is a better way to do this.
it 'renders something' do
expect(response.body).to match("Oh Noes!")
end
end
Assuming it's a request spec, the request will return HTTP 404, and you can set an expectation for that:
is_expected.to be_not_found
Side note:
#user = User.find_by(confirmation_token: step5_params[:confirmation_token])
raise ActiveRecord::RecordNotFound unless #user
can be simplified to just:
#user = User.find_by!(confirmation_token: step5_params[:confirmation_token])
Im trying to write some tests to check before validation of a model in rspec
class MyClass < ApplicationRecord
before_validation :generate_anonymous_id
def generate_anonymous_id
retries ||= 1
self.uuid = SecureRandom.uuid
self.aid = Digest::SHA256.hexdigest(uuid)[0...15]
raise ActiveRecord::RecordNotUnique if OtherClass.find_by_sfid(aid) ||
MyClass.find_by_aid(aid)
rescue ActiveRecord::RecordNotUnique => error
Rails.logger.warn("Encountered duplicate uuid/aid")
retries += 1
retry if retries <= 3
Rails.logger.warn("Raising exception after 3 retries")
raise error
end
end
here is my rspec
# frozen_string_literal: true
require "rails_helper"
RSpec.describe MyClass, type: :model do
describe "model validation" do
let(:my_class) do
MyClass.create
end
context "valid" do
it "allows to create a user aid" do
expect(MyClass.new.valid?).to be_truthy
end
end
context "duplicate" do
subject do
MyClass.new
end
it "allows to create a user aid" do
subject.uuid = my_class.aid
expect(subject.valid?).to be_falsey
end
end
end
end
Im trying to test the rescue block and my test always passes. I'm not able to override my subject and i dont know what is the mistake im doing.
Thanks in advance
Your before_validation overwrites the subject.uuid, which is happening when you call subject.valid? i.e. generate_anonymous_id forces it to be valid
I stubbed the code which raises the exception and it worked.
context "invalid record" do
context "capture exception" do
let!(:user) { create(:custom_user) }
before { allow(UserUuid).to receive(:find_by_sfid).and_return(true)}
subject { described_class.new}
it "raises exception" do
expect { subject.valid? }.to raise_error(ActiveRecord::RecordNotUnique)
end
end
end
I have Sidekiq worker.
class DeliverSmsMessageWorker
include Sidekiq::Worker
def perform(sms_message_id)
....
rescue StandardError => e
Rails.logger.error("SmsMessageWorker ERROR: #{e}")
Bugsnag.notify(e)
end
end
And i write spec, but i get error when i try test Rails.looger.
describe DeliverSmsMessageWorker, type: :worker do
subject(:worker) { DeliverSmsMessageWorker }
context 'on exceptions' do
let(:error) { StandardError.new('test exception') }
before do
allow(worker).to receive(:perform_async).with(sms_message.id).and_raise(error)
end
it 'message in logger' do
Sidekiq::Testing.inline! do
worker.perform_async(sms_message.id)
expect(Rails.logger).to receive(:error).and_call_original
end
end
end
end
After when i run this specs, I get the error. but why?
Is there any point in testing these two lines?
1) DeliverSmsMessageWorker on exceptions message in the logger
Failure/Error: worker.perform_async(sms_message.id)
StandardError:
test exception
Maybe you should use the block syntax for raise_error here:
Sidekiq::Testing.inline! do
expect { worker.perform_async(sms_message.id) }.to raise_error { |error|
expect(Rails.logger).to receive(:error).and_call_original
}
end
I am trying to create an RSpec test which detects if a request can crash the controller, usually a 500 error. So I want to be able to distinguish between:
nil.invalid_method # raises NoMethodError
from
params.require(:required_parameter) # raises ActionController::ParameterMissing
in a controller in a generic way. When I do a request,feature or controller test it raises an exception:
describe "Post", type: :request do
it 'does not crash when no params given' do
post '/posts' # this line launches an exception
expect(page).to_not have_http_status(500)
end
end
It seems that before RSpec (or Rails I don't know) had a different behaviour, similar to I'm looking for:
rails 4 api rspec test http status code 410
Rspec shows different status code than browser
How to use HTTP status code symbols in RSpec?
How can I do this? Or how would you do?
Thanks for your time.
You can use a controller spec that doesn't render a 500, but raises the exception instead:
describe "PostController", type: :controller do
describe "POST index" do
it 'does not crash with valid params' do
expect {
post :index, { post: { title: 'foo' } }
}.to_not raise_exception
end
end
describe "POST index" do
it 'crashes without params' do
expect {
post :index
}.to raise_exception(ActionController::ParameterMissing)
end
end
end
Also note the curly brackets { ... } after expect.
You can test that the controller does not raise an uncaught exception by using the raise_error matcher:
RSpec.describe "Things", type: :request do
describe "POST /things" do
it "does not raise an error" do
# we pass a block to expect
expect { post things_path }.to_not raise_error
end
end
end
If the exception is rescued in the controller by using the rescue keyword or Rails rescue_from you would test the response code as usual:
class ThingsController < ApplicationController
rescue_from ActionController::ParameterMissing do
head 500
end
def create
raise ActionController::ParameterMissing.new('foo')
end
end
RSpec.describe "Things", type: :request do
describe "POST /things" do
it "work even if the param is not provided" do
post things_path
expect(response).to successful
end
end
end
In this case it is much more useful to test that the response is what you expected it to be - not that it is not a 500.
I have the following class, that I am trying to write a spec for:
module IntegrationError
class Error < StandardError; end
class BadRequest < IntegrationError::Error; end
class LogicProblem < IntegrationError::Error; end
def raise_logic_error!(message)
raise IntegrationError::LogicProblem, message
rescue => e
Rails.logger.error e.message
e.backtrace.each do |line|
Rails.logger.error line if line.include?('integrations')
end
end
def raise_bad_request!(message)
raise IntegrationError::BadRequest, message
end
def log_bad_request!(message)
Rails.logger.info message
end
end
with spec
RSpec.describe 'IntegrationError', type: :integration do
let!(:klass) { Class.new { include IntegrationError } }
describe '#log_bad_request!' do
it 'logs it' do
expect(klass.new.log_bad_request!('TESTME')).to be_truthy
end
end
describe '#raise_bad_request!' do
it 'raises it' do
binding.pry
expect(klass.new.raise_bad_request!('TESTME')).to raise_error
end
end
end
the raise_bad_request test returns the error instead of true. Anyone have thoughts on how to write this better to it passes?
I'm using Rails 4 and Rspec 3.4.
If I recall correctly, I believe you need to pass the expectation a block when your raising, like this:
describe '#raise_bad_request!' do
it 'raises it' do
binding.pry
expect{klass.new.raise_bad_request!('TESTME')}.to raise_error
end
end
See docs here
For the raise_error matcher you need to pass a block to expect instead of a value:
expect { klass.raise_bad_request!('TESTME') }.to raise_error
That should do it!