Rspec: How to test an exception is raised in private method?
try and_raise(Product::StaleObjectError.new(nil, nil))
see this question i asked a while about the same issue:
Rspec - wrong number of arguments when raising error
Rspec: Getting error while testing private method
According to the documentation for expect_any_instance_of, this will receive the class as a method argument, so you should use parenthesis instead of curly braces:
it 'test private method called' do
expect_any_instance_of(Product).to receive(:private_method)
...
end
How to write rspec test case to test successful execution of a method and exception condition also?
You should use stubs or should_receive here.
case 1 (the behavior when internet connection exists):
it "should connect to gmail, if internet connection exists" do
Gmail.should_receive(:new)
@mail_sender = MailSender.new
-> { @mail_sender.connect_to_gmail}.should_not raise_error
end
maybe you would like to return some object (Gmail.should_receive(:new).and_return(some_object)
) and continue to work with this stubbed object
case 2 (the behavior when internet connection does not exist):
it "should raise error to gmail, if internet connection does not exist" do
Gmail.stub(:new) { raise SocketError }
@mail_sender = MailSender.new
-> { @mail_sender.connect_to_gmail}.should raise_error
end
I hope this code helps you
Should I test private methods using RSpec?
You can find an in-depth discussion of that very subject in these slides from a Sandi Metz talk.
https://speakerdeck.com/skmetz/magic-tricks-of-testing-railsconf
She says that you may test-drive your private methods if you like, but that the only test that you should worry about are the ones testing the public interface. Otherwise you may be coupling too tightly to implementation.
I think the point by toch, on splitting out service and value object and putting those under tests is also a good one if you are getting nervous about complex private methods that aren't tested.
Rails 4 - How to mock the exception with custom response using rspec?
I would mock it like this:
allow(RestClient).to receive(:post).and_raise(User::APIError, 'custom error message')
See docs about mocking errors
You can then test that the exception is raised with a specific message like this.
expect { method_calling_the_API }
.to raise_error(an_instance_of(User::APIError)
.and having_attributes(message: 'custom error message'))
See RSpec docs for error matcher.
Related Topics
Why Must I Explicitly Call Self on Accessor When Using the Array Union Operator |= in Ruby
My Classes Can't Use Shoes Methods Like Para
Error When Pushing to Heroku - ...Appear in Group - Ruby on Rails
Nomethoderror on Section 5.7 of Rails Guide
Ruby Selenium Web Drive: How to Find Specific Element by Xpath Div Id and CSS Class
Convert This Xml Request to a Proper Savon Request
Unexpected =>, Expecting '}' in Rspec Expect
Module and Class with the Same Name in Rails Project
Gracefully Shutting Down Sidekiq Processes
Rails 4 Use Application Helpers Inside Initializers
Editing JSON Array Contents in Ruby
Ruby Implementation Win32API Get Mouse Scroll/Wheel Input
Rails: How to Use Scope to Find an Element in Array of Arrays
Converting Colors (Not Images) with Imagemagick
Nomethoderror: Undefined Method '[]' for #<Activerecord::Migration:0X000000033Fedc0>