Topic: Drying shoulda functional tests

Hi,

I am trying to solve my very repetitive functional tests code - I use shoulda and its great macros, however I would like to catch the alway used parts of testing code into one place, without having neccessay to explicitely call it - the best way for me isnt another shoulda macro (like the deprecated should_be_restful, which is exactly what I mean - I want to test the basic CRUD operations on many similar controllers), I would like to use some abstract test case. Doesn't someone have a nice example? What I did is almost what I need, but for an unknown reason the tests are runned twice smile

Thanks for any kind of advice.

class AbstractControllerTest < ActionController::TestCase

  logged_in_as :admin do
    context "index" do
      setup do
        get :index
      end

      should_render_template :index
    end
  end

end

class Admin::LabelsControllerTest < AbstractControllerTest
end

Re: Drying shoulda functional tests

You can have more information on why should_be_restful has been removed here
https://thoughtbot.lighthouseapp.com/pr … be_restful

Anyway, for your code, *I* would rather play with nested context

class AbstractControllerTest < ActionController::TestCase
context "handle :index" do
  context "logged as user"
   setup do
    logged_as(:user)
    get :index
   end
    should_redirect_to("Root page"){root_path()}
  end
  context "logged as admin"
   setup do
    logged_as(:admin)
    get :index
   end
    should_render_template :index
  end
end
end

This way, you keep your test structured. And the output is readable.

Re: Drying shoulda functional tests

Cannot accomplish a successful test without including the following line at top of file before you class declaration:
     require File.dirname(__FILE__) + '/../../test_helper'
You will also need to include the following line right after class decalaration
     include DesktopUnitTest