jeudi 1 juin 2017

What are best practices for testing Rails applications that read from config files?

I have a Rails application that I am testing with RSpec, and I'm testing some methods that read values from a YAML config file (for example, ssh.yml for configuring an SSH connection). Now I know that some "core" config files like database.yml and secrets.yml have test blocks for values that need to be loaded into the test environment when it runs.

So I am torn between how I should test my methods. On one hand, I think it should mimic how Rails uses the test blocks in the "core" config files, and make a similar test block in my own YAML file.

On the other (perhaps bigger) hand, these Rails values are not being tested; they are simply used for the Rails server initialization. For my methods, I need to test multiple scenarios involving both valid/invalid config values, as well as scenarios where the method result is both correct and incorrect. I know that I can mock the YAML that is being "read" in, and I will have to do so for the multiple scenarios. So in this regard, having a test block in my YAML file doesn't make sense because it only defines one of the scenarios, and I'd have to mock the rest anyway. This suggests I should simply mock every scenario and avoid the actual YAML file completely in testing.

What is the Rails convention concerning this?

Aucun commentaire:

Enregistrer un commentaire