Confusing test results

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:inrun’]:
    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!

Taro wrote:

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?

You shouldn’t really be making more that one request in a test (if you
really have to recreating the controller/request/response objects
would probably do the trick

Fred

…I didn’t exactly understand what you meant, but I think I can
change the tests a bit to make it work as you say.

On Jun 1, 3:10 pm, Frederick C. [email protected]