# Failure unit test

**URL:** <https://www.ruby-forum.com/t/failure-unit-test/160948>\
**Category:** Rails\
**Created:** [February 21, 2009, 9:39am UTC](https://www.ruby-forum.com/t/failure-unit-test/160948 "2009-02-21T09:39:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jobber](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/848f3c/32.png) [@jobber](https://www.ruby-forum.com/u/jobber)\
**Post date:** [February 21, 2009, 9:39am UTC](https://www.ruby-forum.com/t/failure-unit-test/160948/1 "2009-02-21T09:39:05Z")

</div>

Hello all,

i have a problem with unit test always

here my code for create a table  
1 def test\_should\_create\_a\_member  
2 sp = Member.new(:name =\> “Arnold”, :surname =\> “Stallone”, :street  
=\> “My Street”, :location =\> “Washington”)  
3 assert sp.save # here is failure

4 end

if i make the test, its ok, without failures  
then i write next test:  
1 def test\_name\_should\_be\_unique  
2 sp = Member.new(:name =\> “Silvester”, :surname =\> “Mc Name”, :street  
=\> “Our Street”, :location =\> “Customs”)  
3 assert sp.save  
4 sp = Member.new(:name =\> “Silvester”, :surname =\> “Field”,  
:street =\> “This Street”, :location =\> “Living”)  
5 assert !sp.save  
6 end

if i make tesst there ist 1 failure on line 3 in  
test\_should\_create\_a\_member.

can someone see any false, why this is so now?

please help

---

<div class="post-metadata">

**Author:** ![jobber](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/848f3c/32.png) [@jobber](https://www.ruby-forum.com/u/jobber)\
**Post date:** [February 21, 2009, 11:03am UTC](https://www.ruby-forum.com/t/failure-unit-test/160948/2 "2009-02-21T11:03:30Z")

</div>

edit: when i type for line 3 “assert !sp.save” there is no failure, but  
then  
the test is not right, or?

---

<div class="post-metadata">

**Author:** ![jobber](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/848f3c/32.png) [@jobber](https://www.ruby-forum.com/u/jobber)\
**Post date:** [February 21, 2009, 3:34pm UTC](https://www.ruby-forum.com/t/failure-unit-test/160948/3 "2009-02-21T15:34:29Z")

</div>

Hello G. wrote:

> edit: when i type for line 3 “assert !sp.save” there is no failure, but  
> then  
> the test is not right, or?

All test cases follow some variation this pattern:

assemble - put together the objects you need  
activate - the “money line” - the one that calls production code to  
test  
assert - check the money line did the right thing.

Your test has two money lines, but that’s not the actual problem.

The problem is assert() sucks, and does not tell you what’s wrong.

To test “did my object save correctly?”, always use this:

sp.save! # with the bang !

never this:

assert sp.save

The reason is the first one will tell you what is going wrong. Maybe  
your new()  
arguments do not satisfy one of your validations.

–  
Phlip
