Im writing simple library that check's that mysql server is alive and dependent from results, it do other things. To check connection, I use such code:
def check_connection
result = if @password
`mysqladmin -u#{@username} -p#{@password} ping`
else
`mysqladmin -u#{@username} ping`
end
parse_result(result)
end
How to test this method? I think, I should not connect to mysql server during the test. Only idea I have is to return in one method appropriate string command for ping (depends of password usage) and use it in method like:
def check_connection(ping_string)
`#{ping_string}`
end
and in every test only mock this method, thus only this method use command.
What would you do to test it properly?
You can stick with your original code, and approach it like this:
parse_result is unit tested.check_connection so that the rest of your tests don't end up triggering a call to mysqladmin.There's a hole in that the connection to mysql itself isn't tested, but I don't think that's a big deal. Testing ping_string won't really plug that hole, and given that the call to mysqladmin is basically hard coded your risk here is small.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With