Skip to content

Cannot use controller behaviour test methods: get, post, etc.. #12

Closed
richardkmichael opened this Issue Sep 16, 2011 · 10 comments

5 participants

@richardkmichael

I'm uncertain if this is an issue, or simply a misunderstanding on my part.

I am trying to use ActionController::TestCase::Behaviour methods, such as get, post, etc.. This results in a NoMethodError:

NoMethodError: undefined method 'get' for #<FoosControllerTest:0x000001053a5718>
    test/controllers/foos_controller_test.rb:65:in 'block in <class:FoosControllerTest>'

In minitest-rails/lib/minitest/rails/controller.rb, I uncommented require 'action_controller' and include ActionController::TestCase::Behaviours to load the module, but now I receive a different NoMethodError:

ruby-1.9.2-p290@foo/gems/actionpack-3.1.0/lib/action_controller/test_case.rb:12:in 'block in <module:TemplateAssertions>': undefined method 'setup' for MiniTest::Rails::Controller:Class (NoMethodError)
    from ruby-1.9.2-p290@foo/gems/activesupport-3.1.0/lib/active_support/concern.rb:122:in 'class_eval'

I explicitly added a def setup ; ... ; end to my FooControllerTest class (because currently tests are in the MiniTest::Spec DSL using before do, but the errors persist.

Thank you for minitest-rails.

(In the meantime, I've quickly switched to the mini_specunit shim. However, I'm confused about the different minitest rails gems.)

@richardkmichael

I've resolved the missing setup() method by requiring / including the ActiveSupport::Testing::SetupAndTeardown module.

https://github.com/rails/rails/blob/master/activesupport/lib/active_support/test_case.rb#L28

require 'active_support/testing/setup_and_teardown'

module MiniTest
  module Rails
    class Controller < Spec
      include ActiveSupport::Testing::SetupAndTeardown
    end
  end
end

However, I'm still resolving the get, post, etc. methods, as now there's a nil ivar, @routes.

But I'll keep investigating and hopefully produce a pull request for this soon.

@tomash
tomash commented Oct 17, 2011

I believe these are the droids we're looking for:
https://github.com/rails/rails/blob/master/actionpack/lib/action_controller/test_case.rb

But I'm not fluent enough with rails-core codebase to know right now what should we include and/or subclass :/

@richardkmichael
include ActionController::TestCase::Behavior

Will give these methods. So while I'm trying to resolve all this, I've done this in my test file:

module MiniTest
  module Rails
    class Controller < Spec
      include ActiveSupport::Testing::SetupAndTeardown
      include ActionController::TestCase::Behavior
    end
  end
end

The ivars (@routes, @request, etc.) problem I mentioned above is a standard Rails3 problem, you must require 'rails/test_help'.

@tomash
tomash commented Oct 17, 2011

so, does including these make functional tests work in minitest-rails?

@richardkmichael

Not for me, no. As @phiggins commented on your issue, minitest-rails is quite new. I've just been commenting here while trying to understand how this all works and possibly contribute to "fix" it.

@tomash If you haven't seen it, there's also this project: https://github.com/metaskills/mini_specunit

@carlosramireziii

I was able to get this to work by adding some code snippets from wojtekmach's minitest-rails-example application

Inside of minitest_helper.rb

class MiniTest::Rails::Spec

  # Add methods to be used by all specs here...

  def build_message(*args)
    args[1].gsub(/\?/, '%s') % args[2..-1]
  end
end

class MiniTest::Rails::Controller
  include ActiveSupport::Testing::SetupAndTeardown
  include ActionController::TestCase::Behavior
  before do
    @routes = Rails.application.routes
  end
end

I was then able to use the ActionController::TestCase::Behavior methods in a test

require "minitest_helper"

class PhraseCountersControllerTest < MiniTest::Rails::Controller
  before do
    @controller = PhraseCountersController.new
  end

  describe "#new" do
    it "must render new template" do
      get :new
      assert_template :new
    end
  end
end

Not sure what the final resolution to the problem should be but maybe this will help.

@richardkmichael

Just FYI on this entire issue, MiniTest::Spec has landed in edge rails (4.0 pre). http://www.youtube.com/watch?v=1ieWRX0mPbI

@mhoran
mhoran commented Jan 31, 2012

Also looking for a proper solution to this. I went with something similar to what @carlosramireziii recommended. There were quite a few hiccups, but I finally got this working.

Note that the biggest hiccup was that requiring rails/test_help implicitly requires test/unit, which breaks if you're running minitest with Ruby 1.8. I came across the minitest_tu_shim gem which got these tests running in 1.8.

@blowmage
Owner

Is this still an issue in 0.0.7?

@blowmage
Owner

Improvements coming in 0.1.0.

@blowmage blowmage closed this May 25, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.