# Simple unit test failure

**URL:** <https://www.ruby-forum.com/t/simple-unit-test-failure/138554>\
**Category:** Rails\
**Created:** [May 24, 2008, 6:27pm UTC](https://www.ruby-forum.com/t/simple-unit-test-failure/138554 "2008-05-24T18:27:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [May 24, 2008, 6:27pm UTC](https://www.ruby-forum.com/t/simple-unit-test-failure/138554/1 "2008-05-24T18:27:40Z")

</div>

Snipping out unnecessary, /test/unit/personnel\_test.rb…

def setup  
@first = “Test”  
@last = “User”  
@new\_id = ‘1000’  
end

def test\_create\_read\_update\_delete  
person = Personnel.new(:id =\> @new\_id,  
:first\_name =\> @first,  
:last\_name =\> @last)

```
# save him
assert person.save

```

end

running this test gets me an error…

$ ruby test/unit/personnel\_test.rb  
Loaded suite test/unit/personnel\_test  
Started  
F.  
Finished in 0.39329 seconds.

1. Failure:  
test\_create\_read\_update\_delete(PersonnelTest)  
[test/unit/personnel\_test.rb:23]:  
 is not true.

2 tests, 4 assertions, 1 failures, 0 errors

The error is on the line… ‘assert person.save’

The only ‘not null’ fields in my personnel table are id (obviously),  
first\_name and last\_name. The personnels table does not have a record  
with id = 1000. I can ‘find’ by id items loaded via fixtures but cannot  
assert the above save. Is it my syntax? How do I get more information  
about the failure (adding -v doesn’t give me meaningful info)

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [May 24, 2008, 6:47pm UTC](https://www.ruby-forum.com/t/simple-unit-test-failure/138554/2 "2008-05-24T18:47:03Z")

</div>

Craig,

You could look in log/test.log. That file can grow large enough that  
it’s a little hard to spot just what you want, though, so I often  
delete it before running tests when I need to dig into a test failure.  
It’ll be recreated when you run your test and should contain only the  
data collected from that test run.

Regards,  
Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [May 24, 2008, 6:58pm UTC](https://www.ruby-forum.com/t/simple-unit-test-failure/138554/3 "2008-05-24T18:58:46Z")

</div>

On Sat, 2008-05-24 at 12:46 -0400, Craig D. wrote:

> Craig,
> 
> You could look in log/test.log. That file can grow large enough that  
> it’s a little hard to spot just what you want, though, so I often  
> delete it before running tests when I need to dig into a test failure.  
> It’ll be recreated when you run your test and should contain only the  
> data collected from that test run.

* * *

I’ve been ‘tailing’ it as I work and it gives me nothing…

$ cat log/test.log  
SQL (0.001977) SET search\_path TO db  
SQL (0.001476) BEGIN  
SQL (0.023013) SELECT count(_) AS count\_all FROM personnels  
SQL (0.001639) ROLLBACK  
SQL (0.000325) BEGIN  
SQL (0.002350) SELECT count(_) AS count\_all FROM personnels  
SQL (0.000546) ROLLBACK

the 'select count(\*) are from my ‘teardown’ method

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [May 24, 2008, 7:22pm UTC](https://www.ruby-forum.com/t/simple-unit-test-failure/138554/4 "2008-05-24T19:22:51Z")

</div>

On Sat, 2008-05-24 at 09:26 -0700, Craig W. wrote:

> ```
> :first_name => @first,
> 
> ```
> 
> Started  
> The error is on the line… ‘assert person.save’
> 
> The only ‘not null’ fields in my personnel table are id (obviously),  
> first\_name and last\_name. The personnels table does not have a record  
> with id = 1000. I can ‘find’ by id items loaded via fixtures but cannot  
> assert the above save. Is it my syntax? How do I get more information  
> about the failure (adding -v doesn’t give me meaningful info)

* * *

never mind…it was a validation in the model that wasn’t reflected in  
the database - fixed

Thanks

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [May 24, 2008, 7:59pm UTC](https://www.ruby-forum.com/t/simple-unit-test-failure/138554/5 "2008-05-24T19:59:51Z")

</div>

> > def setup  
> > @first = “Test”  
> > @last = “User”  
> > @new\_id = ‘1000’  
> > end

Going forward, you are abusing the setup here. Each test case should  
generally use _different_ sample data, so put them in the test case  
itself,  
and don’t lean on @ variables until they serve a real purpose!

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [May 24, 2008, 9:37pm UTC](https://www.ruby-forum.com/t/simple-unit-test-failure/138554/6 "2008-05-24T21:37:29Z")

</div>

Craig W. wrote:

> thanks…I’ve been going though the ‘Guide to Testing the Rails’ on  
> [manuals.rubyonrails.com](http://manuals.rubyonrails.com) and ‘Gluttonous’ but it would help to see real  
> life examples…any suggestions?

The source code to beast is exemplary!

> Also, and this is probably big…I use an RBAC method suggested in Rails  
> Recipes with a self-rolled authentication system against LDAP which  
> blocks controllers methods. I can only get these tests to run if I  
> disable the RBAC…any suggestions here?

Use Mocha to make the library return what you need. The point of tests  
is not to  
promise accuracy, it’s to make sure your code stays the same (bugs and  
all) as  
you add features.

And to let you Undo, if a test fails unexpectedly, instead of debugging.

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [May 24, 2008, 8:25pm UTC](https://www.ruby-forum.com/t/simple-unit-test-failure/138554/7 "2008-05-24T20:25:05Z")

</div>

On Sat, 2008-05-24 at 10:58 -0700, Phlip wrote:

> > > def setup  
> > > @first = “Test”  
> > > @last = “User”  
> > > @new\_id = ‘1000’  
> > > end
> 
> Going forward, you are abusing the setup here. Each test case should  
> generally use _different_ sample data, so put them in the test case itself,  
> and don’t lean on @ variables until they serve a real purpose!

* * *

thanks…I’ve been going though the ‘Guide to Testing the Rails’ on  
[manuals.rubyonrails.com](http://manuals.rubyonrails.com) and ‘Gluttonous’ but it would help to see real  
life examples…any suggestions?

Also, and this is probably big…I use an RBAC method suggested in Rails  
Recipes with a self-rolled authentication system against LDAP which  
blocks controllers methods. I can only get these tests to run if I  
disable the RBAC…any suggestions here?

Thanks

Craig
