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

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

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

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

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

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

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

Colin

hi Colin

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

hi Colin

1) Failure:
test_for_update(CountyTest) [test/unit/county_test.rb:19]:
Name has already been taken.
<false> 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

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

hi Colin

  1. Failure:

test_for_update(CountyTest) [test/unit/county_test.rb:19]:

Name has already been taken.

is not true.

your answer

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

Yes
i have validates_uniqueness_of :name

what i need to do
please help

karthik.k

Hi Colin

I got it

we need to give like below

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

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

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

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

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 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.

Please help

--
Karthik.k
Mobile - +91-9894991640

> 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]:
> <nil> 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

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 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.

Please help

Karthik.k

Mobile - +91-9894991640

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.

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:

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

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

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

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