views:

57

answers:

1

One trick I have found very handy in rails application programming is that class_eval can be used to create methods on the fly. I'm starting to get into testing now and I wonder if a similar idea could be used to generate tests.

For example, I have a before_filter to require a user to be logged in for all of the actions in a controller. I would like to write tests which ensure that the before_filter is being applied for all actions. Instead of writing out each test individually I would like to instead generate all of these test automatically.

Is this type of testing advisable, or should I just stick to writing the tests out individually? If it is, how would one go about doing so?

EDIT: This might look something like:

actions = {:index => :get,:show => :get,:edit => :get,:update => :put}
actions.each_pair do |action,type|
  class_eval(%Q{def test_user_required_for_#{action}
      set_active_user users(:one)
      #{type} :#{action}
      assert flash[:error]
      assert_redirected_to :action => :index
    end
  })
end

Now that people have verified this could be useful, where would I put a block of code such as this so that it will get executed once and only once to create these tests?

+1  A: 

The DRY principle applies to test code just as much as it applies to application code.

Having one method to generate all of those tests should make it easier to verify that the test is correct in the first place.

To answer the comment (note: I haven't written Rails test code in a while, so it probably isn't 100% correct). Everything between %| | is one big string:

MyControllerTest

  [:index, :show, :new, :create, :edit, :update, :destroy].each do |action|
      class_eval do %|
        test "#{action} requires before filter" do
          #test #{action} code here
        end
      |
  end

end
erik
How/where would one go about creating these tests then?
Bryan Ward
Where could you put this code so that it gets executed once and only once to setup these test?
Bryan Ward
This belongs in your whichever controller you want to test. For example, if you wanted to make sure the AccountsController is locked down, the code would go in the accounts_controller_test.rb file.
erik
Ok, great that works! I had been trying to write a constructor and put it in there, but you are right you just have to throw it straight into the class, not a separate method call. Thanks!
Bryan Ward