Skip to content

Instantly share code, notes, and snippets.

@wrburgess
Created April 21, 2012 21:42
Show Gist options
  • Save wrburgess/2439800 to your computer and use it in GitHub Desktop.
Save wrburgess/2439800 to your computer and use it in GitHub Desktop.
CodeSchool - Rails Testing for Zombies Notes #test #testunit #rails

CodeSchool Rails Testing for Zombies Notes

Test::Unit Assertions

assert_respond_to [object], :[method] #does object method exist? ex. assert_respond_to "string", :lowercase
assert_equal [string, [method] #is first string equal to results of second method?
assert_match [regex], [method] #is first regex equal to second string?
assert_nil
assert_not_nil [method] #is method nil?  
assert_raise(RuntimeError), [method]  
assert_respond_to
assert_no_match
assert_kind_of(Class, object)

Unit Test Examples

def test_whatever_you_want_to_call_your_test do
  assert_response_to "string", :lowercase, "This is the error message"
end

class ConditionalTest < Test::Unit::TestCase
  def test_one_greater_than_zero
    assert 1 > 0, "One is greater than zero"
  end
end

Model Testing Examples

Prep the test database before running tests:

rake db:te­st:prepare

Run db:test:prepare and all tests:

rake test

Testing if the model is valid without a particular field:

class ModelTest < ActiveSupport::TestCase
  test "model is invalid without a status" do
    model = Model.new
    assert !model.valid?, "Without a status, the model is not valid"
  end
end

Shoulda Testing Examples

Testing for status length:

should ensure_length_of(:status).is_at_least(3).is_at_most(140)

Testing for numeral and uniqueness:

should validate_numericality_of(:id)
should validate_uniqueness_of(:id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment