← Back to Upcase

What to test in a Rails model?

Hi guys,

I was reading at the code from Learn and found something here Announcement spec that cached my attention.

It is a good idea to test for implementation details like the definition of columns? Associations and validations? and if that’s the case why?

Normally what I try to do is test behavior that implies some of those things from that particular behavior, I feel like this test is testing Rails not the business logic for the Learn app. Let me know what you guys think on this thoughts.


1 Like

Hi Rafael,

I stopped testing model validations, as I deem this as testing the Rails framework. I only want to test my own business logic. But someone might disagree with this. I am happy to know why I should test model validations and other functionality provided by the Rails framework.



1 Like

@Rafael_George, generally speaking when TDD you’re going to write tests for the either the validation or the behavior around validations at some level of your specs, either integration, or unit.

Where you have them sort of depends on how the behavior manifests itself in your application. When you’re doing outside-in development, you may add the spec at your integration level and then forego the validation spec at the unit level. I think it’s most common for us to have a basic integration spec that specifies the behavior we expect to see in the UI when validations fail, and then more extensive unit tests that specify the specific validation functionality we want to have in our application.

The important thing is that you are not testing that the validations are working correctly, you are testing that your application in fact has the validations you expect it to have. A subtle, but important, distinction.


@cpytel Totally agree, the thing is that when I saw this specs it’s looks like the validation is testing implementation and at that level looks like kinda redudant to me; maybe I’m wrong because I don’t know what was the situation for this particular code.

In the case of that particular spec, the # Database we would not test it that way now. This app is a 4+ year old code base and certain practices fall in and out of favor over time. We don’t rewrite code we aren’t touching against new guidelines. You can see from the blame view that that code was written in 2012: https://github.com/thoughtbot/learn/blame/master/spec/models/announcement_spec.rb

@cpytel I see your approach now regarding old code; even when this code is 4 years old I’m still learning stuffs and can compare on how you guys did things back then and how are you guys doing it now; thanks for all the clear explanation :wink: