method undefined in integration test

Hello list,

I’m having problems with the following integration test (tests for the authentication aspects of the application):

require “#{File.dirname(FILE)}/…/test_helper”

class AuthenticationTest < ActionController::IntegrationTest

fixtures :your, :models

def test_successful_login george = enter_site(:george)

end

module BrowsingTestDSL include ERB::Util attr_writer :name

def tries_to_go_to_admin
  get "/admin/book/new"
  assert_response :redirect
  assert_redirected_to "/account/login"
end

def enter_site(name)
  open_session do |session|
    session.extend(BrowsingTestDSL)
    [session.name](http://session.name) = name
    yield session if block_given?
  end

end

end

end

When I run this test, rails tell me test_site() is undefined, however, I’ve declared it inside the private module (I’m using the module to implement this test’s DSL, as seen in the book Beginning Ruby on Rails E-Commerce from Apress).

Here’s the output:

"Loaded suite test/integration/authentication_test Started E Finished in 0.312 seconds.

  1. Error: test_successful_login(AuthenticationTest): NoMethodError: undefined method `enter_site’ for #ActionController::Integration::Session:0x4751620
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/test_process.rb:452:in `meod_missing'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/integration.rb:547:in `sen

c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/integration.rb:547:in `metd_missing'
test/integration/authentication_test.rb:7:in `test_successful_login'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-

1.13.3/lib/action_controller/integration.rb:453:in `run 1 tests, 0 assertions, 0 failures, 1 errors"

What am I doing wrong ?

Thanks in advance,

Marcelo.

Hi --

Hello list,

I'm having problems with the following integration test (tests for the authentication aspects of the application):

require "#{File.dirname(__FILE__)}/../test_helper"

class AuthenticationTest < ActionController::IntegrationTest # fixtures :your, :models

def test_successful_login    george = enter_site(:george)

end

module BrowsingTestDSL    include ERB::Util    attr_writer :name

   def tries_to_go_to_admin      get "/admin/book/new"      assert_response :redirect      assert_redirected_to "/account/login"    end

   def enter_site(name)      open_session do |session|        session.extend(BrowsingTestDSL)        session.name = name        yield session if block_given?      end

end

end

end

When I run this test, rails tell me test_site() is undefined, however, I've declared it inside the private module (I'm using the module to implement this test's DSL, as seen in the book Beginning Ruby on Rails E-Commerce from Apress).

Here's the output:

"Loaded suite test/integration/authentication_test Started E Finished in 0.312 seconds. 1) Error: test_successful_login(AuthenticationTest): NoMethodError: undefined method `enter_site' for #<ActionController::Integration::Session:0x4751620>    c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/test_process.rb:452:in `meod_missing'    c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/integration.rb:547:in `sen    c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/integration.rb:547:in `metd_missing'    test/integration/authentication_test.rb:7:in `test_successful_login'    c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/integration.rb:453:in `run 1 tests, 0 assertions, 0 failures, 1 errors"

What am I doing wrong ?

Your module isn't included (in the sense of "include ModuleName") in your class. So instances of your class do not have the module in their method lookup path.

Here's a very small version, with the include instruction added:

   class C      def x        y      end

     module M        def y          puts "hi"        end      end

     include M # This is the key line    end

The fact that the module is nested in the class doesn't matter; it's still just a module (C::M in my case), and it has to be included in any class that wants its objects to have access to it.

David