Functional test case and the setup method

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)

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

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....

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

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 Denner wrote:

I think this is because of the ActiveRecord::Fixtures class defining
method_added.

[snip]