# Unit Testing Conditional Validation

**URL:** <https://www.ruby-forum.com/t/unit-testing-conditional-validation/131054>\
**Category:** Rails\
**Created:** [March 5, 2008, 5:20pm UTC](https://www.ruby-forum.com/t/unit-testing-conditional-validation/131054 "2008-03-05T17:20:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sjglazer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@sjglazer](https://www.ruby-forum.com/u/sjglazer)\
**Post date:** [March 5, 2008, 5:20pm UTC](https://www.ruby-forum.com/t/unit-testing-conditional-validation/131054/1 "2008-03-05T17:20:54Z")

</div>

Hi there,

I have the following conditional validation in my model:

validates\_presence\_of :ext\_name,  
:if =\> :is\_ext?

def is\_ext?  
ext  
end

ext is a boolean value in the database. If it is true the ext\_name need  
to be supplied. Now it appears that the validation is in fact working  
however I wrote two unit test to make sure:

def test\_restaurant\_with\_no\_external\_delivery  
restaurant = Restaurant.new(:ext =\> 0)

assert !restaurant.errors.invalid?(:ext\_name)

end

def test\_restaurant\_with\_external\_delivery

restaurant = Restaurant.new(:ext =\> 1)

assert restaurant.errors.invalid?(:ext\_name)

end

For some reason the second test also fails. Effectively(if i were to  
believe th e test) setting the ext field to true does not in fact  
require the presence of ext\_name. Can some one please help me understand  
what is going wrong here?

Thanks a lot,  
steve

---

<div class="post-metadata">

**Author:** ![sjglazer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@sjglazer](https://www.ruby-forum.com/u/sjglazer)\
**Post date:** [March 5, 2008, 6:10pm UTC](https://www.ruby-forum.com/t/unit-testing-conditional-validation/131054/2 "2008-03-05T18:10:12Z")

</div>

On 5 Mar 2008, at 16:20, Steve G. wrote:

> For some reason the second test also fails. Effectively(if i were to  
> believe th e test) setting the ext field to true does not in fact  
> require the presence of ext\_name. Can some one please help me  
> understand  
> what is going wrong here?

You should be setting ext to true or false, not 1 or 0.  
Fred

---

<div class="post-metadata">

**Author:** ![sjglazer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@sjglazer](https://www.ruby-forum.com/u/sjglazer)\
**Post date:** [March 5, 2008, 6:23pm UTC](https://www.ruby-forum.com/t/unit-testing-conditional-validation/131054/3 "2008-03-05T18:23:05Z")

</div>

Thanks for the response Fred however still no dice. I tried the  
following three variants(separately of course) and the second test  
still failed in each case:

restaurant = Restaurant.new(:ext =\> “false”)  
restaurant = Restaurant.new(:ext =\> ‘false’)  
restaurant = Restaurant.new(:ext =\> false)

I did the same for true in the second case as well. Any other ideas?

Frederick C. wrote:

> On 5 Mar 2008, at 16:20, Steve G. wrote:
> 
> > For some reason the second test also fails. Effectively(if i were to  
> > believe th e test) setting the ext field to true does not in fact  
> > require the presence of ext\_name. Can some one please help me  
> > understand  
> > what is going wrong here?
> 
> You should be setting ext to true or false, not 1 or 0.  
> Fred

---

<div class="post-metadata">

**Author:** ![sjglazer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@sjglazer](https://www.ruby-forum.com/u/sjglazer)\
**Post date:** [March 5, 2008, 6:37pm UTC](https://www.ruby-forum.com/t/unit-testing-conditional-validation/131054/4 "2008-03-05T18:37:56Z")

</div>

It was indeed the fact that I wasn’t calling valid? first. Thanks a lot  
for your help Fred it is much appreciated.

Frederick C. wrote:

> On 5 Mar 2008, at 17:23, Steve G. wrote:
> 
> > Thanks for the response Fred however still no dice. I tried the  
> > following three variants(separately of course) and the second test  
> > still failed in each case:
> > 
> > restaurant = Restaurant.new(:ext =\> “false”)  
> > restaurant = Restaurant.new(:ext =\> ‘false’)  
> > restaurant = Restaurant.new(:ext =\> false)
> 
> You definitely want the last one. The other thing is that the errors  
> object is only set up when the valid? method has been called.
> 
> Fred

---

<div class="post-metadata">

**Author:** ![sjglazer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@sjglazer](https://www.ruby-forum.com/u/sjglazer)\
**Post date:** [March 5, 2008, 6:31pm UTC](https://www.ruby-forum.com/t/unit-testing-conditional-validation/131054/5 "2008-03-05T18:31:24Z")

</div>

On 5 Mar 2008, at 17:23, Steve G. wrote:

> Thanks for the response Fred however still no dice. I tried the  
> following three variants(separately of course) and the second test  
> still failed in each case:
> 
> restaurant = Restaurant.new(:ext =\> “false”)  
> restaurant = Restaurant.new(:ext =\> ‘false’)  
> restaurant = Restaurant.new(:ext =\> false)

You definitely want the last one. The other thing is that the errors  
object is only set up when the valid? method has been called.

Fred
