# About assert\_difference problem. help!

**URL:** <https://www.ruby-forum.com/t/about-assert-difference-problem-help/113629>\
**Category:** Ruby\
**Created:** [September 3, 2007, 4:46am UTC](https://www.ruby-forum.com/t/about-assert-difference-problem-help/113629 "2007-09-03T04:46:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![zlai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/b3f665/32.png) [@zlai](https://www.ruby-forum.com/u/zlai)\
**Post date:** [September 3, 2007, 4:46am UTC](https://www.ruby-forum.com/t/about-assert-difference-problem-help/113629/1 "2007-09-03T04:46:43Z")

</div>

class PersonTest \< Test::Unit::TestCase  
fixtures :people  
def setup  
end

def test\_should\_create\_person  
assert\_difference people, :count do  
person=Person.newly\_created  
assert !person.new\_record?,  
“#{person.errors.full\_messages.to\_sentence}”  
end  
end  
end

people.yml  
test1:  
id: 1  
user\_id: 1  
firstname: test1  
lastname: YY  
test2:  
id: 2  
user\_id: 2  
firstname: test2  
lastname: TT

ruby test\unit\person\_test.rb

1. Error:  
test\_should\_create\_person(PersonTest):  
ArgumentError: wrong number of arguments (0 for 1)  
test/unit/person\_test.rb:16:in `people' test/unit/person_test.rb:16:in`test\_should\_create\_person’

1 tests, 0 assertions, 0 failures, 1 errors

I don’t know why causeed this proble. who can tell me? Thanks.

---

<div class="post-metadata">

**Author:** ![zlai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/b3f665/32.png) [@zlai](https://www.ruby-forum.com/u/zlai)\
**Post date:** [September 3, 2007, 5:11am UTC](https://www.ruby-forum.com/t/about-assert-difference-problem-help/113629/2 "2007-09-03T05:11:23Z")

</div>

OnRails R. wrote:

> def test\_should\_create\_person  
> assert\_difference people, :count do  
> person=Person.newly\_created

How is assert\_difference expected to inflect “people” into “Person”? Or  
is  
people declared somewhere else?

Use assert\_latest:

person = assert\_latest Person do  
Person.newly\_created # ← you supply this, right?  
end  
assert person.new\_record?

Note that assert\_latest returns nil, one item, or an [] array of items.  
So  
calling .new\_record? on its return value is itself a test that one item  
appeared.

> ```
> assert !person.new_record?,
> 
> ```
> 
> “#{person.errors.full\_messages.to\_sentence}”

That line says to flunk if person is not a new record; is this what you  
wanted?

> people.yml  
> test1:

Both assert\_difference (which comes from various plugins, not from  
Rails)  
and assert\_latest (which comes from

> **[O'Reilly Media - Technology and Business Training](https://www.oreilly.com)**
>
> Gain technology and business knowledge and hone your skills with learning resources created and curated by O'Reilly's experts: live online training, video, books, our platform has content from 200+ of the world's best publishers.

)  
don’t detect fixtures, because they appear just before setup() time.

---

<div class="post-metadata">

**Author:** ![zlai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/b3f665/32.png) [@zlai](https://www.ruby-forum.com/u/zlai)\
**Post date:** [September 3, 2007, 6:13am UTC](https://www.ruby-forum.com/t/about-assert-difference-problem-help/113629/3 "2007-09-03T06:13:01Z")

</div>

I just want to do unit/test, test to creat person data into the table  
“people” of model person. and in model person, there is “def  
Person.newly\_created(args)” method.  
In fixtures file, there is  
test1:  
id: 1  
user\_id: 1  
firstname: test1  
lastname: YY  
test2:  
id: 2  
user\_id: 2  
firstname: test2  
lastname: TT  
I want to use assert\_difference to test creat all data into database.  
and now I modified as blow:  
def test\_should\_create\_person  
assert\_difference Person, :count do  
assert=Person.newly\_created.valid?  
end  
end  
but there is same error:

1. Error:  
test\_should\_create\_person(PersonTest):  
ArgumentError: wrong number of arguments (0 for 1)  
test/unit/person\_test.rb:17:in `newly_created' test/unit/person_test.rb:17:in`test\_should\_create\_person’  
D:/kisscafe/config/…/lib/authenticated\_test\_helper.rb:42:in  
`assert_differe nce' test/unit/person_test.rb:16:in`test\_should\_create\_person’

1 tests, 0 assertions, 0 failures, 1 errors

So I don’t know how to fix this problem.

By the way, I have read assert\_latest, but I don’t think that can  
perform my target.  
ThanKs, Phlip

---

<div class="post-metadata">

**Author:** ![zlai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/b3f665/32.png) [@zlai](https://www.ruby-forum.com/u/zlai)\
**Post date:** [September 3, 2007, 7:43am UTC](https://www.ruby-forum.com/t/about-assert-difference-problem-help/113629/4 "2007-09-03T07:43:47Z")

</div>

OnRails R. wrote:

> I just want to do unit/test, test to creat person data into the table  
> “people” of model person. and in model person, there is “def  
> Person.newly\_created(args)” method.  
> In fixtures file, there is  
> test1:  
> id: 1  
> user\_id: 1  
> firstname: test1  
> lastname: YY  
> test2:  
> id: 2  
> user\_id: 2  
> firstname: test2  
> lastname: TT  
> I want to use assert\_difference to test creat all data into database.  
> and now I modified as blow:  
> def test\_should\_create\_person  
> assert\_difference Person, :count do  
> assert.Person.newly\_created.valid?  
> end  
> end  
> but there is same error:
> 
> 1. Error:  
> test\_should\_create\_person(PersonTest):  
> ArgumentError: wrong number of arguments (0 for 1)  
> test/unit/person\_test.rb:17:in `newly_created' test/unit/person_test.rb:17:in`test\_should\_create\_person’  
> D:/kisscafe/config/…/lib/authenticated\_test\_helper.rb:42:in  
> `assert_differe nce' test/unit/person_test.rb:16:in`test\_should\_create\_person’
> 
> 1 tests, 0 assertions, 0 failures, 1 errors
> 
> So I don’t know how to fix this problem.
> 
> By the way, I have read assert\_latest, but I don’t think that can  
> perform my target.  
> ThanKs, P

def test\_should\_create\_person  
assert\_difference Person, :count do  
assert.Person.newly\_created.valid?  
end  
end

---

<div class="post-metadata">

**Author:** ![zlai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/b3f665/32.png) [@zlai](https://www.ruby-forum.com/u/zlai)\
**Post date:** [September 25, 2007, 11:00pm UTC](https://www.ruby-forum.com/t/about-assert-difference-problem-help/113629/5 "2007-09-25T23:00:12Z")

</div>

Thank you very much, Phlip  
I got it.  
Thanks!

---

<div class="post-metadata">

**Author:** ![zlai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/b3f665/32.png) [@zlai](https://www.ruby-forum.com/u/zlai)\
**Post date:** [September 25, 2007, 11:03pm UTC](https://www.ruby-forum.com/t/about-assert-difference-problem-help/113629/6 "2007-09-25T23:03:52Z")

</div>

Thank you so much, Phlip.  
I am new learner for test. and you help me so much.  
I dealed with assert\_difference depend on your suggestion.  
The first time I just used “assert\_difference Person, :count do”  
all the data have inserted database that is I wanted.  
and second time I want to verify these data, I used  
“assert.Person.newly\_created.valid?”, but it is wrong.  
So could you give me some suggestion about this?  
Thanks again!

---

<div class="post-metadata">

**Author:** ![zlai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/b3f665/32.png) [@zlai](https://www.ruby-forum.com/u/zlai)\
**Post date:** [September 25, 2007, 11:04pm UTC](https://www.ruby-forum.com/t/about-assert-difference-problem-help/113629/7 "2007-09-25T23:04:03Z")

</div>

> “assert.Person.newly\_created.valid?”, but it is wrong.

You wrote “assert.Person”. There’s a dot between the assert and Person.

Use “assert Person”!

–  
Phlip  
[http://www.oreilly.com/catalog/9780596510657/](http://www.oreilly.com/catalog/9780596510657/)  
“Test Driven Ajax (on Rails)”  
assert\_xpath, assert\_javascript, & assert\_ajax

---

<div class="post-metadata">

**Author:** ![zlai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/b3f665/32.png) [@zlai](https://www.ruby-forum.com/u/zlai)\
**Post date:** [September 25, 2007, 11:10pm UTC](https://www.ruby-forum.com/t/about-assert-difference-problem-help/113629/8 "2007-09-25T23:10:22Z")

</div>

[Please don’t reply to private e-mail addresses from a public group…]

OnRails wrote:

> def test\_should\_create\_person  
> assert\_difference Person, :count do  
> assert.Person.newly\_created.valid?

What’s with the dot . between assert and Person?

Tip: Write almost nothing in your test. Then run

```
rake test:recent

```

and see it pass. Then write the _minimum_ new code into your test that  
you  
possibly can. Run test:recent again. Keep repeating until you understand  
each failure point. Don’t write a whole bunch and wait to test it all.

That technique is how experienced programmers write huge programs,  
too…

–  
Phlip  
[http://www.oreilly.com/catalog/9780596510657/](http://www.oreilly.com/catalog/9780596510657/)  
“Test Driven Ajax (on Rails)”  
assert\_xpath, assert\_javascript, & assert\_ajax
