ruby on rails - How to test code that uses system commands -


I am writing a simple library which checks that the mysql server is alive and is dependent on the results, it does other things is. To check the connection, I use this type of code:

  def check_connection result = if @password `mysqladmin -u # {@ username} -p # {@ password} ping `Else` mysqladmin -u # {@username} ping` end parse_result (results) end   

How to test this method? I think, I should not connect to mysql server during testing. Only one idea is to return a method to the ping (password usage dependency) in the appropriate string command and use it in the method:

  def check_conference (ping_string) `# {ping_string } 'End'   

and simulate this method only in every exam, so just use this method to command.

What will you do to test it properly?

You can paste it from your original code, and view it like this:

  1. Ensure that the parse_result unit is tested.
  2. Fake check_connection so that your remaining tests are mysqladmin .

    There is a hole in it that is not investigated in connection with mysql, but I do not think this is a big deal. The test ping_string will not really plug that hole, and given that call to mysqladmin is basically hard coded here, your risks are small here.

Comments