How can I test with RSpec some code with #exit!?
def method
...
rescue MyError => e
logger.error "FATAL ERROR"
exit! 1
end
I can test this code with #exit method because raise the SystemExit exception.
it "logs a fatal error" do
lambda do
object.method
expect(logger).to have_received(:error).with("FATAL ERROR")
end
end
it "exits" do
expect { object.method }.to raise_error(SystemExit)
end
I'm not sure if I can achieve something similar. I'm thinking to reimplement the exit! method in Kernel module, just only for the specs. Any ideas?
You can stub the exit! method for the object:
it "logs a fatal error" do
lambda do
allow(object).to receive(:exit!)
object.method
expect(logger).to have_received(:error).with("FATAL ERROR")
end
end
it "exits" do
expect(object).to receive(:exit!)
object.method
end
Related
I have an rspec where I am testing if an exception thrown from method1 is rescued and raised again in method2 (method2 calls method1). But the RSpec seems to fail. I have debugged through the code and can see the exception being raised.
it 'should check for exceptions raised' do
allow(TestClass).to receive(:method1) do
raise ::Test::CustomException.new(1, 'test.csv.gz', StandardError.new('test exception'))
end
expect(#test_class.method2).to raise_error(Test::CustomException)
end
method2
tester["key_list"].each do |key|
begin
TestClass.method1(key, self.id) do |row|
#do something
end
rescue Test::CustomException => exception
raise ::Test::CustomException.new(self.id, key, exception)
end
end
method1
begin
#do something
rescue => exc
raise ::Test::CustomException.new(id, key, exc)
end
end
CustomException as the name says is a custom defined exception that takes in 3 parameters
Error:
Failures:
should check for exceptions raised
Failure/Error: expect(#test_class.method2).to raise_error
Test::CustomException:
true
test_class.rb:146:in `rescue in block method'
Is there a way to rspec test if an error was raised and rescued? If I have a rescue, my rspec test does not see the raised error, only results in the rescue?
module MyApp
def some_method(msg)
raise StandardError.new(msg)
end
def second_method(msg)
begin
count = 0
some_method(msg)
rescue StandardError=> e
puts e
count = 1
end
end
end
RSpec.describe Myapp do
describe "#some_method" do
it "should raise error" do
expect {
some_method("this is an error")
}.to raise_error(StandardError) {|e|
expect(e.message).to eql "this is an error"
}
end
end
# this fails, as the error is not raised
describe "#second_method" do
it should raise error and rescue do
expect {
a = second_method("this is an error and rescue")
}.to raise_error(StandardError) {|e|
expect(e.message).to eql "this is an error and rescue"
expect(a) = 1
}
end
end
end
You generally don't want to raise or rescue StandardError directly because it's pretty uninformative, and won't catch errors outside of the StandardError hierarchy. Instead, you generally want to test that a specific exception was raised, or that a specific error class or error message was raised.
If you know the custom or built-in exception class that you want, or the specific error message, then test for that explicitly. For example:
it 'should raise an ArgumentError exception' do
expect { MyApp.new.foo }.to raise_error(ArgumentError)
end
it 'should raise MyCustomError' do
expect { MyApp.new.foo }.to raise_error(MyCustomError)
end
it 'should raise StandardError with a custom message' do
msg = 'this is a custom error and rescue'
expect { MyApp.new.foo }.to raise_error(msg)
end
If you don't know (or care about) the specific exception or message that should be raised, but you expect some exception to interrupt the execution flow, then you should use a bare raise_error matcher. For example:
it "should raise an exception" do
expect { MyApp.new.foo }.to raise_error
end
How can you test that a method catches a thrown symbol in RSpec? I have two methods that interact with each other through #throw and #catch. I already figured out how to test that the symbol is thrown on one end:
expect { subject.method_a }.to throw_symbol(:some_symbol)
Now I want to test that method_b catches the thrown symbol, which I imagine might look something like this:
expect { subject.method_b }.to catch_symbol(:some_symbol)
Only that doesn't work. So my question is, how can you test that a method catches a symbol in RSpec?
EDIT: Here's a very basic example of method_a and method_b, stripped of all logic not related to the problem at hand.
def method_a
throw :some_symbol
end
def method_b
catch :some_symbol do
method_a
end
end
catch and throw are methods on Kernel so you can expect them as usual:
class SomeClass
def a
throw :foo
end
def b
catch :foo do
a
end
end
end
RSpec.describe "" do
it "" do
inst = SomeClass.new
expect(inst).to receive(:throw).with(:foo)
inst.a
expect(inst).to receive(:catch).with(:foo)
inst.b
end
end
Is there any way to get access to an error message in a rescue block as a string? For example:
def foo
raise RuntimeError, "This is an error"
end
def bar
begin
foo
rescue RuntimeError
puts "Rescued"
end
end
bar
Is there any way to gain access to "This is an error" from with-in the rescue block? Something like this:
...
rescue RuntimeError
puts <error-message>
end
...
You need to specify a variable to store the error in
def foo
raise RuntimeError, "This is an error"
end
def bar
begin
foo
rescue RuntimeError => ex
puts "Rescued #{ex.message}"
end
end
I have a method that calls two other methods:
def first_method
second_method
# Don´t call this method when something went wrong before
third_method
end
The second_method calls other methods:
def second_method
fourth_method
fifth_method
end
Let´s say the fifth_method has a begin/rescue statement:
def fifth_method
begin
# do_something
rescue Error => e
#
end
end
Now I want to avoid third_method to be called when fifth_method throws an error. How would I/you solve this most elegantly in Ruby.
It seems to me so obvious but anyway
def first_method
begin
second_method
rescue
return
end
third_method
end
This construction (without explicit type of exception) will catch StandartError exception.
To avoid intersection with another exceptions you can create your own exception class:
class MyError < StandardError; end
and then use it
begin
second_method
rescue MyError => e
return
end
Note that you should not inherit exception from Exception because this type of exceptions are from environment level, where the exceptions of StandardError are meant to deal with application level errors.
I think the simplest way is removing error catching from fifth_method and move it to the first_method
def first_method
begin
second_method
third_method
rescue Error => e
end
end
def fifth_method
# do_something
end
If you don't want to use exceptions, you can just return a status:
def fifth_method
# do_something
true
rescue Error => e
false
end
def first_method
if second_method
third_method
end
end