# rSpec and model validations

**URL:** <https://www.ruby-forum.com/t/rspec-and-model-validations/146084>\
**Category:** Rails\
**Created:** [August 17, 2008, 4:57pm UTC](https://www.ruby-forum.com/t/rspec-and-model-validations/146084 "2008-08-17T16:57:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![matid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b4bc9f/32.png) [@matid](https://www.ruby-forum.com/u/matid)\
**Post date:** [August 17, 2008, 4:57pm UTC](https://www.ruby-forum.com/t/rspec-and-model-validations/146084/1 "2008-08-17T16:57:55Z")

</div>

Hi there, I’ve got a little question regarding rspec and the way I ought  
to test models.  
Let’s say I’ve got the following test: [5797’s gists · GitHub](http://gist.github.com/5797)  
It ensures the identity model is validated and returns an error if the  
login is too short.  
This test doesn’t cover one thing though: if I change  
validates\_lenght\_of :login, :minimum =\> 4 to validates\_length\_of :login,  
:minimum =\> 4, :on =\> :create it still passes, but I’d like it to fail.  
The most straight-forward solution would be to add a specification with  
update\_identity instead of new\_identity to see whether this validation  
is run on update.  
This doesn’t seem dry though, as I need to do some copy & paste.  
Is there any preferable solution to this kind of problem?

---

<div class="post-metadata">

**Author:** ![matid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b4bc9f/32.png) [@matid](https://www.ruby-forum.com/u/matid)\
**Post date:** [August 17, 2008, 7:05pm UTC](https://www.ruby-forum.com/t/rspec-and-model-validations/146084/2 "2008-08-17T19:05:29Z")

</div>

Do the most straight-forward thing. DRY is not a useful concern in  
writing tests. Being explicit about the behavior of the code under  
test is more important. Test that the login is valid on update in  
either case. Test that it is valid on create when 4 or longer and  
invalid when 3 or less.

If you change the behavior of your code, you’ll have to write new  
tests for the new behavior. That’s sort of the point of testing.

On Aug 17, 10:57 am, Mateusz Drozdzynski \<rails-mailing-l…@andreas-
