I have a method:
require 'openssl'
def extract_creds(data)
pfx = OpenSSL::PKCS12.new(data)
{ :certificate => pfx.certificate.to_pem, :key => pfx.key.to_pem }
rescue
# handle error
end
and i want to write a rspec example for it. How should i properly mock pfx object?
Why this may be the wrong question
You should generally use a mock when you want to:
In this case, it’s not clear why you want to test this behavior, or what you expect the results to be, or why you think you might not get it. For the most part, testing a well-tested external library is not the right thing to do.
What I think the right question is
So, depending on what you really want to test, you may want to check:
In both cases, though, there seems to be no real benefit to mocking the library vs. including a real certificate as fixture data. Fixtures are better than mocks for actually exercising code.