Ruby on Rails Testing
Assertion Cheat Sheet

Last week I was working on the Rails testing book and I started wondering how the rest of the Rails world was writing their tests. Since I decided that I was going to use Test::Unit instead of Rspec in the book, I knew that I could safely limit my search to just assertions and refutions.

The first thing I did was identify every method available to ActiveSupport::TestCase that starts with assert or refute. Once I had that list of 55 methods, I searched through github for instances of that method in files that end in ‘.rb’ nested in a test directory.

The results were impressive. There were 1,449,068 assertions and refutions in Ruby projects. I figured that it would be helpful to use this data to make a cheat sheet for assertions available in Ruby on Rails, ordered by popularity.

Assertions:

  • assert

    (test, message = nil)
  • assert_not_nil

    (test, message = nil)
  • assert_equal

    (expected, actual, message = nil)
  • assert_difference

    (expression, difference = 1, message = nil, &block)
  • assert_nil

    (object, message = nil)
  • assert_raise

    (*args, &block)
  • assert_match

    (pattern, string, message = nil)
  • assert_kind_of

    (class, object, message = nil)
  • assert_nothing_raised

    (*args, &block)
  • assert_no_difference

    (expression, message = nil, &block)
  • assert_raises

    (*args, &block)
  • assert_not_equal

    (expected, actual, message = nil)
  • assert_instance_of

    (class, object, message = nil)
  • assert_include

    (collection, object, message = nil)
  • assert_respond_to

    (object, method, message = "")
  • assert_in_delta

    (expected_float, actual_float, delta = 0.001, message = "")
  • assert_same

    (expected, actual, message = "")
  • assert_no_match

    (pattern, string, message = "")
  • assert_empty

    (object, message = nil)
  • assert_includes

    (collection, object, msg = nil)
  • assert_operator

    (object1, operator, object2, message = "")
  • assert_block

    (message = "", &block)
  • assert_not_include

    (collection, object, message = nil)
  • assert_not_same

    (expected, actual, message = "")
  • assert_not_empty

    (object, message = nil)
  • assert_not_match

    (pattern, string, message = "")
  • assert_nothing_thrown

    (message = "", &block)
  • assert_not_includes

    (collection, object, message = nil)
  • assert_send

    (send_array, message = nil)
  • assert_present

    (object, message = nil)
  • assert_blank

    (object, message = nil)
  • assert_throws

    (*args, &block)
  • assert_output

    (stdout = nil, stderr = nil)
  • assert_silent

    (stdout = nil, stderr = nil)
  • assert_not_respond_to

    (actual, method, message="")
  • assert_in_epsilon

    (expected_float, actual_float, epsilon = 0.001, message = "")
  • assert_not_send

    (send_array, message = nil)
  • assert_not_operator

    (object1, operator, object2, message = "")
  • assert_not_in_delta

    (expected_float, actual_float, delta = 0.001, message = "")
  • assert_not_kind_of

    (class, object, message = "")
  • assert_not_in_epsilon

    (expected_float, actual_float, epsilon = 0.001, message = "")
  • assert_not_instance_of

    (class, object, message = "")

Refutions:

  • refute

    (test, message = nil)
  • refute_equal

    (expected, actual, message = nil)
  • refute_match

    (pattern, string, message = nil)
  • refute_nil

    (object, message = nil)
  • refute_empty

    (object, message = nil)
  • refute_includes

    (collection, object, message = nil)
  • refute_same

    (expected, actual, message = "")
  • refute_operator

    (object1, operator, object2, message = "")
  • refute_respond_to

    (object, method, message = "")
  • refute_kind_of

    (class, object, message = nil)
  • refute_instance_of

    (class, object, message = nil)
  • refute_in_epsilon

    (expected_float, actual_float, epsilon = 0.001, message = "")
  • refute_in_delta

    (expected_float, actual_float, delta = 0.001, message = "")

Do you want to learn how to test your Rails apps?
I'm writing a book about it! Sign up for updates.

Avatar

Hi. I'm Eric.

I've been building things with Rails for three years, and trying to figure out how to test for almost as long. By day, I'm a Rails product consultant, and by night, well I guess I'm still a Rails product consultant, only it's darker outside. You should follow me @genericsteele.