Skip to content

Instantly share code, notes, and snippets.

@wejrowski
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wejrowski/9979508 to your computer and use it in GitHub Desktop.
Save wejrowski/9979508 to your computer and use it in GitHub Desktop.
Rspec guides notes

General guides

  • Use describe blocks for objects and methods ("given" scenarios)
  • Use context blocks for "when" scenarios
  • Avoid spec descriptions longer than 40 characters. Split into contexts if longer.
  • Prefer expect syntax
  • Use before blocks at the lowest scope necessary
    • e.g. start within it blocks, then move into describe, then only if necessary for all tests put outside of those.
  • Use validation tests and place them at the top of the describe block
  • Declare instance method tests first then class methods
  • Use "#instance_method" at the beginning of instance method tests
  • Use ".class_method" at the beginning of class method tests

Examples:

describe Admin::MyController do
  describe "GET #edit" do
    it "responds with 200 success" do
      # code
    end
  end
end
describe User do

  it { should validate_presence_of(:first_name) }
  it { should belong_to(:account) }

  describe "#instance_method" do
    context "when a variation is happening" do
      before(:each) do
        # code
      end 

      it "does x" do
        # code
      end

      it "does y" do
        # code
      end
    end
  end

  describe ".class_method" do
    # code
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment