Realización de pruebas semiautomatizadas con ruby

I am writing an open source gem that interacts with an sms service. I want to test the interaction, however it needs account information and a phone number to run. It also needs feedback to determine if sms messages were being sent correctly. This causes two problems:

  1. I can't put the account information in the test file, as the gem is open source and anyone could get to it.

  2. I need the person running the test to give information to the script as it is running (eg checking the phone to see if a message was received).

What techniques or libraries are available that can help with this? I'm currently using rspec and making it prompt for parameters (using gets), however it is pretty cluncky at the moment. I can't be the first person using ruby to have this problem, and I feel that I'm missing a gem or something that solves this problem.

preguntado el 27 de agosto de 11 a las 15:08

3 Respuestas

Use mocks

What are your tests testing, specifically? That a given login/password works? Probably not. Most likely you want to make sure your code reacts to the API properly. Therefore, I'd suggest mocking. Save the output of the API calls and use a mock service to return those responses. Then test. Your tests will be faster and less brittle as a happy side-effect.

More information on mocking with RSpec is here:

Respondido el 20 de junio de 20 a las 12:06

The tests are making sure the api is correctly interfacing with the base http api that the service uses. Therefor a mock isn't very useful as I really need to make sure the interface is working correctly. - David Miani

Do you need to make sure the service is up and functioning correctly at the time of the unit test? I wouldn't recommend this. See also - Mark Thomas

Hmm... re-reading your post, are you trying to use RSpec for something otros than unit testing? If so, can you be more specific about the type of testing and include when the tests will be run and by whom? - Mark Thomas

The library is pretty small (it is at ). It is really an functional/integration test, as I need to make sure that using the library actually connects to the service. Ie I need to make sure that using conn.send_sms("hello", "12345") sends "hello" to the mobile number "12345". If the service's http api changes, then I need the test to fail. The tests will be run by me, and by the wire2air service developer (who I am developing it for) to make sure that the library actually works. - David Miani

Re 1) Why not just save configuration options in a YAML file and load them at the beginning of your tests?

Re 2) Are there maybe any web services for that? E.g. one where you can send a message to and query an API to see if it worked. I know this can be unreliable, but the same is true for a user's phone company network.

Respondido 27 ago 11, 20:08

Even if there aren't, it's not a problem to create one running on localhost. - Mchl

Sure, but it sounded like OP wanted other people to run the tests, so having to set up a local web service for them might be overkill. - Michael Kohl

Certainly. That's why the usual solution is to create mocks. - Mchl

1 could work well, I could try reading from a default file, and if it wasn't found, prompt for additional details. 2 is probably overkill, a command line interface is good enough. - David Miani

+1 for Mark Thomas' answer on mocking. Two more alternative mock object libraries for Ruby: FlexMock y Mocha

Respondido 27 ago 11, 21:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.