4

I'm creating a gem that will generate a controller for the Rails app that will use it. It's been a trial and error process for me when trying to test a controller. When testing models, it's been pretty easy, but when testing controllers, ActionController::TestUnit is not included (as described here). I've tried requiring it, and all similar sounding stuff in Rails but it hasn't worked.

What would I need to require in the spec_helper to get the test to work?

Thanks!

2
  • I've brought up this question in rspec-users mailing list, and Aslak Hellesoy said that generally, if you're testing a Rails thing, it's easier to compile the gem, use it in a Rails app and write those tests in the Rails app. While it definitely sounds easier, it feels like it's very broken apart. It will make it brittle when I put this gem out there in the wild. Commented Apr 6, 2010 at 15:27
  • Since then, I've opted to use the dummy app approach. See whilefalse.net/2012/01/25/testing-rails-engines-rspec Commented Oct 23, 2012 at 13:57

1 Answer 1

2

Here's an example of a working standalone Test::Unit test with a simple controller under test included.. Maybe there's some parts here that you need to transfer over to your rspec code.

require 'rubygems' require 'test/unit' require 'active_support' require 'active_support/test_case' require 'action_controller' require 'action_controller/test_process' class UnderTestController < ActionController::Base def index render :text => 'OK' end end ActionController::Routing::Routes.draw {|map| map.resources :under_test } class MyTest < ActionController::TestCase def setup @controller = UnderTestController.new @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new end test "should succeed" do get :index assert_response :success end end 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks I will try this later today and post up how it goes.
It seems the fundamental difference is that I don't think you can make your test class a subclass of ActionController::TestCase in rspec. In rspec I believe it's automatically inferred based on the describe block. eg. describe SampleController, "on GET index" will look for the SampleController in app/controllers. I hope I'm wrong and this can be overridden!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.