# Functional test case and the setup method

**URL:** <https://www.ruby-forum.com/t/functional-test-case-and-the-setup-method/125606>\
**Category:** Rails\
**Created:** [January 10, 2008, 3:41pm UTC](https://www.ruby-forum.com/t/functional-test-case-and-the-setup-method/125606 "2008-01-10T15:41:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![reacher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/0ea827/32.png) [@reacher](https://www.ruby-forum.com/u/reacher)\
**Post date:** [January 10, 2008, 3:41pm UTC](https://www.ruby-forum.com/t/functional-test-case-and-the-setup-method/125606/1 "2008-01-10T15:41:08Z")

</div>

I noticed that in my unit test case, i can define a setup method which  
gets called before all my unit tests. This does not work however for  
functional test cases. Is this intended, or do I have something  
screwed up? I’m running Rails 2.0.2 on WinXP (same thing occurring in  
Ubuntu 7.10)

---

<div class="post-metadata">

**Author:** ![reacher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/0ea827/32.png) [@reacher](https://www.ruby-forum.com/u/reacher)\
**Post date:** [January 10, 2008, 3:51pm UTC](https://www.ruby-forum.com/t/functional-test-case-and-the-setup-method/125606/2 "2008-01-10T15:51:44Z")

</div>

On 10 Jan 2008, at 14:40, Reacher wrote:

> I noticed that in my unit test case, i can define a setup method which  
> gets called before all my unit tests. This does not work however for  
> functional test cases. Is this intended, or do I have something  
> screwed up? I’m running Rails 2.0.2 on WinXP (same thing occurring in  
> Ubuntu 7.10)

This should work. In rails 2 the boilerplate setup method is gone from  
functional tests (since every one had the same 3 lines in them). There  
was an issue where if you defined your own setup method you had to  
call super, but I can’t remember if that was fixed in 2.0.2/2.0.1 or  
if the fix is still only on edge

Fred

---

<div class="post-metadata">

**Author:** ![reacher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/0ea827/32.png) [@reacher](https://www.ruby-forum.com/u/reacher)\
**Post date:** [January 10, 2008, 3:54pm UTC](https://www.ruby-forum.com/t/functional-test-case-and-the-setup-method/125606/3 "2008-01-10T15:54:41Z")

</div>

Hmm my setup method isn’t getting called. It is in my unit test, but  
that class is subclassed from ActiveSupport::TestCase, not  
ActionController::TestCase. Wonder why it isn’t working for me…

On Jan 10, 9:49 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![reacher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/0ea827/32.png) [@reacher](https://www.ruby-forum.com/u/reacher)\
**Post date:** [January 11, 2008, 4:38pm UTC](https://www.ruby-forum.com/t/functional-test-case-and-the-setup-method/125606/4 "2008-01-11T16:38:06Z")

</div>

I guess I’ll peek in the rails library code to see if I can get to the  
bottom of this.

---

<div class="post-metadata">

**Author:** ![reacher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/0ea827/32.png) [@reacher](https://www.ruby-forum.com/u/reacher)\
**Post date:** [January 15, 2008, 3:19pm UTC](https://www.ruby-forum.com/t/functional-test-case-and-the-setup-method/125606/5 "2008-01-15T15:19:06Z")

</div>

On 11/01/2008, Reacher [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I guess I’ll peek in the rails library code to see if I can get to the  
> bottom of this.

I think this is because of the ActiveRecord::Fixtures class defining  
method\_added. If you look at the fixtures.rb file in activerecord  
you’ll find that it does this:

def self.method\_added(method)  
return if @ **disable\_method\_added**  
@ **disable\_method\_added** = true

```
case method.to_s
when 'setup'
  unless method_defined?(:setup_without_fixtures)
    alias_method :setup_without_fixtures, :setup
    define_method(:full_setup) do
      setup_with_fixtures
      setup_without_fixtures
    end
  end
  alias_method :setup, :full_setup
when 'teardown'
  unless method_defined?(:teardown_without_fixtures)
    alias_method :teardown_without_fixtures, :teardown
    define_method(:full_teardown) do
      teardown_without_fixtures
      teardown_with_fixtures
    end
  end
  alias_method :teardown, :full_teardown
end

@ __disable_method_added__ = false

```

end

I personally think that those calls to ‘alias\_method :setup,  
:full\_setup’ and ‘alias\_method :teardown, :full\_teardown’ should be  
inside the unless statements because, as it stands, whenever you get a  
definition of ‘setup’ or ‘teardown’ it will simply overwrite them.  
The code should, IMO, read:

def self.method\_added(method)  
return if @ **disable\_method\_added**  
@ **disable\_method\_added** = true

```
case method.to_s
when 'setup'
  unless method_defined?(:setup_without_fixtures)
    alias_method :setup_without_fixtures, :setup
    define_method(:full_setup) do
      setup_with_fixtures
      setup_without_fixtures
    end
    alias_method :setup, :full_setup
  end
when 'teardown'
  unless method_defined?(:teardown_without_fixtures)
    alias_method :teardown_without_fixtures, :teardown
    define_method(:full_teardown) do
      teardown_without_fixtures
      teardown_with_fixtures
    end
    alias_method :teardown, :full_teardown
  end
end

@ __disable_method_added__ = false

```

end

Matt

---

<div class="post-metadata">

**Author:** ![reacher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/0ea827/32.png) [@reacher](https://www.ruby-forum.com/u/reacher)\
**Post date:** [September 17, 2008, 4:11pm UTC](https://www.ruby-forum.com/t/functional-test-case-and-the-setup-method/125606/6 "2008-09-17T16:11:56Z")

</div>

Hi Folks,

I wanted to share that I was experience the same situation; the ‘setup’  
method in my Functional tests were not getting called when my Functional  
test derived from ActionController::TestCase and I was confused because  
I was referring to pre-Rails 2.0.2 examples that derived from  
Test::Unit::TestCase.

I imagine other folks who are just adding testing goodness to their apps  
may experience the same.

I followed Matt’s suggestion below and made the changes to the  
fixtures.rb file in  
/usr/local/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active\_record  
(as installed in OS/X 10.5.4)

I also added a call to “super” in my functional test setup method and  
got my setup method back in my fixture test.

For the benefit of anyone else who is struggling with this; calling  
super in your setup method sets up the @controller, @request and  
@response so you don’t need to. Therefore, you can use the setup to  
prepare variables that will be used throughout your other functional  
tests.

- Peter DP

Matthew D. wrote:

> I think this is because of the ActiveRecord::Fixtures class defining  
> method\_added.

[snip]

> I personally think that those calls to ‘alias\_method :setup,  
> :full\_setup’ and ‘alias\_method :teardown, :full\_teardown’ should be  
> inside the unless statements because, as it stands, whenever you get a  
> definition of ‘setup’ or ‘teardown’ it will simply overwrite them.  
> The code should, IMO, read:
> 
> def self.method\_added(method)  
> return if @ **disable\_method\_added**  
> @ **disable\_method\_added** = true
> 
> ```
> case method.to_s
> when 'setup'
> unless method_defined?(:setup_without_fixtures)
> alias_method :setup_without_fixtures, :setup
> define_method(:full_setup) do
> setup_with_fixtures
> setup_without_fixtures
> end
> alias_method :setup, :full_setup
> end
> when 'teardown'
> unless method_defined?(:teardown_without_fixtures)
> alias_method :teardown_without_fixtures, :teardown
> define_method(:full_teardown) do
> teardown_without_fixtures
> teardown_with_fixtures
> end
> alias_method :teardown, :full_teardown
> end
> end
> 
> @ __disable_method_added__ = false
> 
> ```
> 
> end
> 
> Matt
