TDD with Validations

I’m completely new to TDD, so forgive my noobishness.

I’m working on app right now that I know will need validations. Would writing a failing test for a validation, then making it green by adding the validation be the proper approach?

As I write it, it seems like the obvious answer (based on my basic understanding of TDD) is yes, but I wanted to be sure. I’m constantly fighting with myself to kick my bad habits and embrace the TDD approach - so convincing myself to not fix X bug with 1 line of code but instead write a failing test is something I’m running into a lot. :stuck_out_tongue:

You should do exactly that. If it’s a critical path for your app (You would lose a lot of money if this validation wasn’t working), you should have an integration test as well. We wrote a gem specifically to make this easier. GitHub - thoughtbot/shoulda-matchers: Simple one-liner tests for common Rails functionality

It also felt weird to me at first but I got used to it pretty fast. As @seangriffin suggests I’m also using shoulda-matchers for shorter syntax.

Just as a note, the important thing is that you’re not testing that rails validations work, you’re testing that you’ve interacting with them properly.

1 Like