About assert_difference problem. help!

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:intest_should_create_person’

1 tests, 0 assertions, 0 failures, 1 errors

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

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

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

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:intest_should_create_person’
    D:/kisscafe/config/…/lib/authenticated_test_helper.rb:42:in
    assert_differe nce' test/unit/person_test.rb:16:intest_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

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:intest_should_create_person’
    D:/kisscafe/config/…/lib/authenticated_test_helper.rb:42:in
    assert_differe nce' test/unit/person_test.rb:16:intest_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

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

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!

“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/
“Test Driven Ajax (on Rails)”
assert_xpath, assert_javascript, & assert_ajax

[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/
“Test Driven Ajax (on Rails)”
assert_xpath, assert_javascript, & assert_ajax