In order to test whether the controller correctly raises the error or
not, I''ve conducted the following test:
-----
def test_delete_default_wiki # this test works...
post :delete_wiki,
{ :lang_url_id => ''en'',
:wiki_id => 1 },
{ :user_id => 5 }
assert_response :redirect
assert_redirected_to :controller => ''admin'',
:action => ''edit'', :lang_url_id =>
''en'',
:wiki_id => 1
assert_equal Wiki.count, 3
assert_equal flash[:notice], "Cannot delete this wiki!"
end
def test_delete_last_wiki # and this doesn''t
assert_equal Wiki.count, 4
# delete one wiki....
post :delete_wiki,
{ :lang_url_id => ''jp'',
:wiki_id => 4 },
{ :user_id => 5 }
assert_response :redirect
assert_redirected_to :controller => ''admin'',
:action => ''edit'', :lang_url_id =>
''jp'',
:wiki_id => 3
assert_equal Wiki.count, 3
assert_nil flash[:notice]
# delete the last wiki!
post :delete_wiki,
{ :lang_url_id => ''jp'',
:wiki_id => 3 },
{ :user_id => 5 }
assert_response :redirect
assert_redirected_to :controller => ''admin'',
:action => ''edit'', :lang_url_id =>
''jp'',
:wiki_id => 3
assert_equal Wiki.count, 3
assert_equal flash[:notice], "Cannot delete the last wiki!" #line
365 (failure line)
end
------
This is the method I''m testing:
------
def delete_wiki
if request.post?
begin
#@wiki.destroy
@lang.wikis.destroy( @wiki )
@wiki = @lang.wikis.first
rescue Exception => e
puts "Error message to set: " + e.message
flash[:notice] = e.message
puts "flash[:notice] now equals: " + flash[:notice]
end
end
redirect_to( :controller => ''admin'',
:action => ''edit'',
:lang_url_id => @lang.url_id,
:wiki_id => @wiki.id )
end
------
The model that''s connected to the error message:
-----
class Wiki < ActiveRecord::Base
#indicate this is array elements
belongs_to :language
acts_as_list :scope => :language_id
#general limits:
validates_presence_of :title
validates_format_of :title, :with => /^\w.+$/ #letter-first
validates_uniqueness_of :title, :scope => :language_id
validates_presence_of :body
validates_length_of :body, :minimum => 10
def before_destroy
raise "Cannot delete the last wiki!" if
Language.find( self.language_id ).wikis.size == 1
end
def after_destroy
raise "Cannot delete this wiki!" if self.id == 1
end
end
-----
And the test result:
-----
Loaded suite admin_controller_test
Started
Error message to set: Cannot delete this wiki!
flash[:notice] now equals: Cannot delete this wiki!
.Error message to set: Cannot delete the last wiki!
flash[:notice] now equals: Cannot delete the last wiki!
F.....................
Finished in 1.468 seconds.
1) Failure:
test_delete_last_wiki(AdminControllerTest)
[admin_controller_test.rb:365:in `test_delete_last_wiki''
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2-/lib/
active_support/testing/default.rb:7:in `run'']:
<nil> expected but was
<"Cannot delete the last wiki!">.
23 tests, 136 assertions, 1 failures, 0 errors
Process ruby exited with code 1
-----
Now, puts shows that the flash[:notice] is set to the correct error
message in the controller, but for some reason, it''s reseted to nil in
the test! More oddly, the first test works with similar procedures,
yet the second doesn''t.... Why is that? Is it due to my redirect
statement?
Thanks in advance!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---