开发者

Helper functions in Rails unit tests

开发者 https://www.devze.com 2023-01-10 17:28 出处:网络
I\'m using this sort of code in my code in my Unit tests. test \"should be开发者_开发问答 awesome\" do

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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消