Question

I'm using this sort of code in my code in my Unit tests.

test "should be awesome" do
  assert true
end

I'm using FactoryGirl instead of fixtures.

I find that I'm repeating myself a lot and having helper functions would be quite useful. What's the best way to create and call a helper function in the unit test? Is there a before_filter available like it is in the controllers (I tried putting it in, but it's just an undefined method). Any help is appreciated!

Was it helpful?

Solution

You can add utility functions to your unit tests without a problem. As long as you don't call them name them like "test_something" they won't get run as unit tests. You can then call them from your actual unit test methods (the format you use boils down to having a method in the class anyway).

So:

test "should be awesome" do
  assert_general_awesomeness
  assert true
end

private

def assert_general_awesomeness
  assert true
end

Utility methods that are going to be used all over the place can go in test_helper and they will be available to all your tests. You could alternatively have a module that you mix in to your tests to provide common utility methods.

If you are doing common calls to set up before a unit test you can put in in a setup method which will be called before each test in the class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top