def test_should_create_county
assert_difference(‘County.count’) do
post :create, :county =>
{:name=>“myname”,:description=>“mydesc”,:region_id=>“3” }
end
assert_redirected_to county_path(assigns(:county))
end
And error message i got is
Failure:
test_should_create_county(CountiesControllerTest)
[/test/functional/counties_controller_test.rb:16]:
“County.count” didn’t change by 1.
<3> expected but was
<2>.
Hi
Can’t say what happens only based on this. Anyway please change
above to
def test_should_create_county
County.destroy_all
assert_difference(‘County.count’) do
post :create, :county =>
{:name=>“myname”,:description=>“mydesc”,:region_id=>“3” }
end
assert_redirected_to county_path(assigns(:county))
end
Ya i saw but no unsatisfied validations is there, i have one doubt
in each controller, there is authentication checked, does that affect
this
functional testing
Yes as Fred said you need that and suppose if you are using a session
varible person_id it can be done like for example in test_helper just
write a def like
def login_as(person) @request.session[:person_id] = person ? person.id : nil
end
And from your test call this by passing administrator object as
variable
Ya i saw but no unsatisfied validations is there, i have one doubt
in each controller, there is authentication checked, does that affect this
functional testing
If your controller normally requires users to be logged in then you
will need to make it appear that the test requests are coming from a
logged in user.
Hi
Can’t say what happens only based on this. Anyway please change
above to
def test_should_create_county
County.destroy_all
That destroy should not matter. (And assert_latest is much more accurate
&
useful than assert_difference here.)
assert_difference('County.count') do
post :create, :county =>
{:name=>“myname”,:description=>“mydesc”,:region_id=>“3” }
end
assert_redirected_to county_path(assigns(:county))
end
Most likely you fail validation. If your error flashes to the page, you
can put
assert_match /something/, flash[:warning], or the equivalent, above the
assert_redirect.
Otherwise, change your save to save! and see if you get a crash message.
If your controller normally requires users to be logged in then you
will need to make it appear that the test requests are coming from a
logged in user.
Fred
thank you Fred
I will do that and let you know
–
Karthik.k
Mobile - +91-9894991640
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.