# Simple app works fine but unit tests don't work as intended

**URL:** <https://www.ruby-forum.com/t/simple-app-works-fine-but-unit-tests-dont-work-as-intended/142609>\
**Category:** Rails\
**Created:** [July 9, 2008, 5:31pm UTC](https://www.ruby-forum.com/t/simple-app-works-fine-but-unit-tests-dont-work-as-intended/142609 "2008-07-09T17:31:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [July 9, 2008, 5:31pm UTC](https://www.ruby-forum.com/t/simple-app-works-fine-but-unit-tests-dont-work-as-intended/142609/1 "2008-07-09T17:31:24Z")

</div>

I have a simple rails 2.0.2 app with one model, Portfolio, and  
generated CRUD. I’ve added validation to the model and tested it  
thoroughly by hand – it works fine.

I tried to create a unit test to ensure no inadvertent future changes  
break my validations.

The relevant code is at [http://www.pastie.org/230714](http://www.pastie.org/230714).

Question 1:  
ruby test/unit/portfolio\_test.rb fails with (essentially)  
portfolio\_test\_rb:13 is not true.

Why did this fail

Question 2:  
I thought perhaps the previous failure occurred because I didn’t save  
the new portfolio. But when I un-comment portfolio\_test.rb:8, I get  
a new error:  
NoMethodError: You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.upcase!  
[snip] app/models/portfolio.rb:9

Thanks in advance for any light you can shine on my errors,  
Richard

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [July 9, 2008, 5:45pm UTC](https://www.ruby-forum.com/t/simple-app-works-fine-but-unit-tests-dont-work-as-intended/142609/2 "2008-07-09T17:45:31Z")

</div>

On 9 Jul 2008, at 16:30, RichardOnRails wrote:

> I have a simple rails 2.0.2 app with one model, Portfolio, and  
> generated CRUD. I’ve added validation to the model and tested it  
> thoroughly by hand – it works fine.
> 
> I tried to create a unit test to ensure no inadvertent future changes  
> break my validations.

Because you shouldn’t use an attr\_accessor with the same name as a  
database attributes. Those attributes are not instance variables.  
Calling attr\_accessor replaces (this is a slight simplification, but  
effectively this is what happens) the .symbol method that rails  
created to read that attribute with one that reads from an instance  
variable. Nothing however populates that instance variable.

Fred

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [July 9, 2008, 6:08pm UTC](https://www.ruby-forum.com/t/simple-app-works-fine-but-unit-tests-dont-work-as-intended/142609/3 "2008-07-09T18:08:57Z")

</div>

Hi Fred,

Wow! Your reply was substantive, comprehensive and fast!

Many thanks,  
Richard

On Jul 9, 11:44 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [July 9, 2008, 6:17pm UTC](https://www.ruby-forum.com/t/simple-app-works-fine-but-unit-tests-dont-work-as-intended/142609/4 "2008-07-09T18:17:10Z")

</div>

Hi Fred,

Wow! Your reply was substantive, comprehensive and fast!

Many thanks,  
Richard

On Jul 9, 11:44 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
