Hi doubt in unit testing

def test_check_for_validity
post=County.new(:name=>“myname”,:description=>“mydesc”)
assert post.save
end

above is the method and when i run unit test it is saying as

  1. Failure:
    test_check_for_validity(CountyTest) [/test/unit/county_test.rb:10]:
    is not true.

what does it say i cannot under stand

please help


Karthik.k
Mobile - +91-9894991640

2009/8/1 karthik k [email protected]:

what does it say i cannot under stand

It is saying that the post.save failed (the assert is expecting true,
so false makes the test fail). Possibly your validations are failing.
If you put the line

assert post.valid?, post.errors.full_messages

before the save this will check the item for validity before
attempting to save it and show you any errors from validations (I
think).

Colin

Hi Colin

Thank you very much

can you please guide me for testing purpose because i am very new to
ruby on
rails


Karthik.k
Mobile - +91-9894991640

Hi Colin

Thank you
I saw that link but as a fresher not able to understand
but if i have any doubt please guide me


Karthik.k
Mobile - +91-9894991640

2009/8/1 karthik k [email protected]:

Hi Colin

Thank you very much

can you please guide me for testing purpose because i am very new to ruby on
rails

Did you try what I suggested?

If you have not already done so I would look at the RoR guides at
http://guides.rubyonrails.org/ particularly Getting Started and
Testing Rails Applications. Also the rest of them in fact.

Colin

On Aug 1, 9:13 am, karthik k [email protected] wrote:

Hi Colin

Thank you
I saw that link but as a fresher not able to understand
but if i have any doubt please guide me

Like Colin said, your test is asserting that the record is saved
successfully. The most likely reason for this failing is if your model
has validations that are not being satisfied (and one way of working
out which one and why is to do as Colin suggested).

Fred

hi Fred

Thank you

I will do the same


Karthik.k
Mobile - +91-9894991640

On Sat, Aug 1, 2009 at 2:13 PM, Frederick C.
<[email protected]

wrote:

Like Colin said, your test is asserting that the record is saved

ruby

assert post.save

think).

Colin


Karthik.k
Mobile - +91-9894991640
http://kkarthikresume.blogspot.com/

hi
i am updating the data in unit testing

def test_for_update
post=counties(:one)
assert post.valid?, post.errors.full_messages
assert post.update_attributes(:name=>"")
end

name should not be empty but as you said i displayed

“assert post.valid?, post.errors.full_messages”

But i am not getting error message in assert but getting error message

please let me know whether i am right or wrong

please help


Karthik.k
Mobile - +91-9894991640

hi Colin

  1. Failure:
    test_for_update(CountyTest) [test/unit/county_test.rb:19]:
    Name has already been taken.
    is not true.


Karthik.k
Mobile - +91-9894991640

2009/8/1 karthik k [email protected]:

name should not be empty but as you said i displayed

“assert post.valid?, post.errors.full_messages”

But i am not getting error message in assert but getting error message

What is the error message you see (all of it)

Colin

2009/8/1 karthik k [email protected]:

hi Colin

  1. Failure:
    test_for_update(CountyTest) [test/unit/county_test.rb:19]:
    Name has already been taken.
    is not true.

Could you reply with your comments inserted into the existing email
please rather than at the top, it makes it much easier to follow the
thread, so your comment above should have been after my bit asking for
the error.

Have you got a validates_uniqueness_of :name? I think the error means
there are two with the same name. If you can’t see the problem post
your counties.yml.

Earlier it was suggested that you look at the rails guides Getting
Started and Testing. Have you done that and do you understand all
that is in them? (Or at least understand most of it)

Colin

My Question

def test_check_for_validity
post=County.new(:name=>“myname”,:description=>“mydesc”)
assert post.save
end

above is the method and when i run unit test it is saying as

  1. Failure:
    test_check_for_validity(CountyTest) [/test/unit/county_test.rb:10]:
    is not true.

what does it say i cannot under stand

please help

your answer

It is saying that the post.save failed (the assert is expecting true,
so false makes the test fail). Possibly your validations are failing.
If you put the line

assert post.valid?, post.errors.full_messages

before the save this will check the item for validity before
attempting to save it and show you any errors from validations (I
think).

Colin

My question

name should not be empty but as you said i displayed

“assert post.valid?, post.errors.full_messages”

But i am not getting error message in assert but getting error message

On Sat, Aug 1, 2009 at 4:26 PM, Colin L. [email protected]
wrote:

your answer

Earlier it was suggested that you look at the rails guides Getting
Started and Testing. Have you done that and do you understand all
that is in them? (Or at least understand most of it)

Colin

Yes
i have validates_uniqueness_of :name

what i need to do
please help

karthik.k

Hi colin

On small help

below is the code or checking for uniquness

def test_check_for_uniqueness_name
post=County.new(:name=>“mynamed”,:description=>“mydesc”,:region_id=>“3”)
assert post.valid?, “post was not valid #{post.errors.inspect}”

post1=County.new(:name=>“sample”,:description=>“mydesc”,:region_id=>“4”)
assert post1.valid?, post1.errors.full_messages
assert_not_nil post1.errors.on(:name)
end

when i run unit testing

  1. Failure:
    test_check_for_uniqueness_name(CountyTest)
    [test/unit/county_test.rb:26]:
    expected to not be nil.

what i am making mistake

can you please guide me


Karthik.k
Mobile - +91-9894991640

2009/8/1 karthik k [email protected]:

post1=County.new(:name=>“sample”,:description=>“mydesc”,:region_id=>“4”)
    assert post1.valid?, post1.errors.full_messages
   assert_not_nil post1.errors.on(:name)
 end

when i run unit testing

  1. Failure:
    test_check_for_uniqueness_name(CountyTest) [test/unit/county_test.rb:26]:
    expected to not be nil.

You have said in the test that you expect post1.errors.on(:name) not
to be nil, but the test fails because it is in fact nil. Why did you
expect it not to be nil?

Colin

Hi Colin

I got it

we need to give like below

assert post.update_attributes(:name=>"") ,post.errors.full_messages


Karthik.k
Mobile - +91-9894991640

Hi Colin

the name field is made as

validates_uniqueness_of :name

so please let me know what i did is right or wrong
though the name is different it is providing the error message

Is this the right way to check uniqueness

I got the information from from

http://railstips.org/2009/1/8/test-or-die-validates-uniqueness-of

Please help


Karthik.k
Mobile - +91-9894991640

2009/8/4 karthik k [email protected]:

I got the information from from

http://railstips.org/2009/1/8/test-or-die-validates-uniqueness-of

Please do not top post, insert your question in the right place in
other peoples responses. It is much easier then to follow the thread.
You have not answered my previous questions. See below.

2009/8/1 karthik k [email protected]:

expected to not be nil.

You have said in the test that you expect post1.errors.on(:name) not
to be nil, but the test fails because it is in fact nil. Â Why did you
expect it not to be nil?

You still have not answered this question - in the test shown why did
you expect post1.errors.on(:name) to be not nil?. When you reply
please put your answer after here so that we can see what you are
replying to:

Also you have not replied to a previous question I asked:

Earlier it was suggested that you look at the rails guides Getting
Started and Testing. Have you done that and do you understand all
that is in them? (Or at least understand most of it)

Colin

On Tue, Aug 4, 2009 at 12:43 PM, Colin L. [email protected]
wrote:

post=County.new(:name=>“mynamed”,:description=>“mydesc”,:region_id=>“3”)

  1. Failure:
    you expect post1.errors.on(:name) to be not nil?. When you reply
    please put your answer after here so that we can see what you are
    replying to:

I got that post from the below link

http://railstips.org/2009/1/8/test-or-die-validates-uniqueness-of

So i tried to use and check

Also you have not replied to a previous question I asked:

Earlier it was suggested that you look at the rails guides Getting
Started and Testing. Have you done that and do you understand all
that is in them? (Or at least understand most of it)

Ya I studied the guide but they did not explain fully
as i am new to ruby on rails i have basic doubt “how it happens”
Though ruby on rails say coding by convention

When i search for any document there are some version conflict i am
using
2.3.2 (as my client needs)

this is where i am standing i am a java developer with struts, flex etc

but ruby on rails is very new so thats why i have this much queries
If i am wrong i am sorry

Colin


Karthik.k
Mobile - +91-9894991640
http://kkarthikresume.blogspot.com/

On Aug 4, 5:05 am, karthik k [email protected] wrote:

Hi Colin

the name field is made as

validates_uniqueness_of :name

Your test is not consistent: you first assert that post1 is valid,
then you assert that it has an error on the name column - clearly
these cannot both be true.

Fred

On Tue, Aug 4, 2009 at 2:19 PM, Frederick C.
<[email protected]

wrote:

Your test is not consistent: you first assert that post1 is valid,
then you assert that it has an error on the name column - clearly
these cannot both be true.

Fred

hi Fred

def test_check_for_uniqueness_name
post=County.new(:name=>“mynamed”,:description=>“mydesc”,:region_id=>“3”)
assert post.valid?, “post was not valid #{post.errors.inspect}”
assert post.save

post1=County.new(:name=>“myname”,:description=>“mydesc”,:region_id=>“4”)
assert post1.valid?, post1.errors.full_messages
assert post1.save
end

above is my code and worked fine for uniqueness

I two name is same then it provides error message else it succeeds

plz let me know whether i am right or wrong