# Validate\_presence\_of unit testing with if condition

**URL:** <https://www.ruby-forum.com/t/validate-presence-of-unit-testing-with-if-condition/120199>\
**Category:** Rails\
**Created:** [November 14, 2007, 6:36pm UTC](https://www.ruby-forum.com/t/validate-presence-of-unit-testing-with-if-condition/120199 "2007-11-14T18:36:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![joan2kus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9fc29f/32.png) [@joan2kus](https://www.ruby-forum.com/u/joan2kus)\
**Post date:** [November 14, 2007, 6:36pm UTC](https://www.ruby-forum.com/t/validate-presence-of-unit-testing-with-if-condition/120199/1 "2007-11-14T18:36:11Z")

</div>

In the layer.rb model, I have

validates\_presence\_of :data\_source  
validates\_presence\_of :url, :if =\> :other\_dataset?

def other\_dataset?  
data\_source == ‘other’  
end

When I tried to do the unit testing,

def test\_when\_datasource\_is\_other  
layer = Layer.new(:data\_source =\>‘other’, :url =\> nil)  
assert !layer.valid?  
assert layer.errors.invalid?(:url)  
end

it keeps throw me an error:  
NoMethodError: undefined method `data\_source=’ for #Layer:0x311bce8

---

<div class="post-metadata">

**Author:** ![joan2kus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9fc29f/32.png) [@joan2kus](https://www.ruby-forum.com/u/joan2kus)\
**Post date:** [November 14, 2007, 6:49pm UTC](https://www.ruby-forum.com/t/validate-presence-of-unit-testing-with-if-condition/120199/2 "2007-11-14T18:49:03Z")

</div>

does your table have a column with the name ‘data\_source’ ?  
if this is only temporary data, you must define it with  
attr\_accessor :data\_source

---

<div class="post-metadata">

**Author:** ![joan2kus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9fc29f/32.png) [@joan2kus](https://www.ruby-forum.com/u/joan2kus)\
**Post date:** [November 14, 2007, 7:01pm UTC](https://www.ruby-forum.com/t/validate-presence-of-unit-testing-with-if-condition/120199/3 "2007-11-14T19:01:35Z")

</div>

Thorsten M. wrote:

> does your table have a column with the name ‘data\_source’ ?  
> if this is only temporary data, you must define it with  
> attr\_accessor :data\_source

Yes, the layers table has a string type column named ‘data\_source’

---

<div class="post-metadata">

**Author:** ![joan2kus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9fc29f/32.png) [@joan2kus](https://www.ruby-forum.com/u/joan2kus)\
**Post date:** [November 15, 2007, 1:25am UTC](https://www.ruby-forum.com/t/validate-presence-of-unit-testing-with-if-condition/120199/4 "2007-11-15T01:25:13Z")

</div>

Ensure your test database is up to date schema-wise.

rake db:test:clone

If you recently did a migrate, that puts your test db out of sync.

I’ve got a plugin I’ll release soon that just makes db:migrate do the  
db:test:clone step automatically (cos I always forget to do that when I  
run  
tests in textmate)
