Skip to content

Instantly share code, notes, and snippets.

@bruschill
Last active August 29, 2015 14:15
Show Gist options
  • Save bruschill/7cb3d751f64a4a61f7d2 to your computer and use it in GitHub Desktop.
Save bruschill/7cb3d751f64a4a61f7d2 to your computer and use it in GitHub Desktop.
Testing Scenarios
#### TESTING SCENARIOS
### GENERAL
# When it comes to testing you'll want to write tests
# for every outcome of anything (method call, object instantiation,
# redirect, etc.) to verify that what you expect to happen actually happens,
# regardless of how rarely you think it may happen.
#
# Ultimately what this boils down to is testing:
# - every condition of a conditional (if/else, case, etc.)
# - method
# - unexpected values passed as parameters
# - return values
#
# Ex.
if some_number is > 0 && some_number < 100
# test the code in this case...
...
elsif some_number > 100
# ...and this case...
...
elsif some_number < 0
# ...and this case...
...
else
# ...and this "extremely rare" case.
...
end
# Ex.
def add(first_number, second_number)
return first_number + second_number
# What if one of the parameters is a string? Or if one of them is nil?
end
### RAILS
## MODELS
# For models you'll want to test:
# - validatons
# - methods
# - expected associations exist
## CONTROLLERS
# For controllers you'll want to test:
# - HTTP status code returned (http://bit.ly/1fFATGL)
# - is the user redirected properly when the action is a success? When it fails?
# - is there a difference in the redirect or what happens in an action when a user is
# signed in? What about when a user isn't signed in?
# - what happens when there's an unexpected error (i.e. ActiveRecord::RecordNotFound)?
### RSPEC
## BEFORE BLOCKS
# You can set up some variables before you run tests, which is super helpful for testing
# most everything, but is indespensible when testing controller actions
#
# ex
describe SomeController do
before(:each) do
# whatever you put here will happen before every test that comes after it. You can:
# - initialize some instance variables
# - create some records in the database
# - set up a hash to use as params before you test a controller action
...
end
it "should pass this test" do
# The test code here will use whatever instance variables you've set up in the before block
...
end
end
## TIPS
# for testing controller actions when a user is logged in and when a user isn't logged in,
# Devise provides some really nice helpers. (http://bit.ly/1wlEjcm, Controller Specs section)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment