# Confusing test results

**URL:** <https://www.ruby-forum.com/t/confusing-test-results/139206>\
**Category:** Rails\
**Created:** [June 1, 2008, 9:03pm UTC](https://www.ruby-forum.com/t/confusing-test-results/139206 "2008-06-01T21:03:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![taro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/df705f/32.png) [@taro](https://www.ruby-forum.com/u/taro)\
**Post date:** [June 1, 2008, 9:03pm UTC](https://www.ruby-forum.com/t/confusing-test-results/139206/1 "2008-06-01T21:03:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![taro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/df705f/32.png) [@taro](https://www.ruby-forum.com/u/taro)\
**Post date:** [June 1, 2008, 9:11pm UTC](https://www.ruby-forum.com/t/confusing-test-results/139206/2 "2008-06-01T21:11:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![taro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/df705f/32.png) [@taro](https://www.ruby-forum.com/u/taro)\
**Post date:** [June 2, 2008, 3:01am UTC](https://www.ruby-forum.com/t/confusing-test-results/139206/3 "2008-06-02T03:01:11Z")

</div>

…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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
