Skip to content

Instantly share code, notes, and snippets.

@edshadi
Forked from ndelage/testing with rspec.md
Created June 4, 2013 15:34
Show Gist options
  • Save edshadi/5706868 to your computer and use it in GitHub Desktop.
Save edshadi/5706868 to your computer and use it in GitHub Desktop.

Definitions

General Testing

  • TDD Test Driven Development. Write examples before implementation.
  • BDD Behaviour-Driven Development is about implementing an application by describing its behavior from the perspective of its stakeholders. (The Rspec Book)
  • RSpec (mention alternatives, write a simple hand sewn test)

Testing Terms

Structure of an example (test)

  • Code Example An executable example of how the subject code can be used and its expected behavior. Also called a test, calling tests code examples reinforces the documentation value of RSpec tests.
  • Example Group defined with the 'describe' method, pass it a string, which is what we'll be describing, this reinforces the idea that Rspec is used to describe and document a system.
  • It A block which defines a code example

Test Implementation

  • Expectations A requirement for any test. In code examples we set expectations of what should happen. If the expectations aren't met, the test fails.

      user.active.should == true
      user.active.should eq(true)
      user.active.should be_true
      
      user.favorites.should include("Ramen")
      user.favorites.should_not include("Guava")
    
  • Mocks/Doubles/Stubs All the same thing. A test double is an object that stands in for another object in an example (test)

      user = mock(:user)
      user = double(:user)
      user = stub(:user)
    
  • Message Stubs Returning a predefined response to a message within a code example

      user.stub(:name).and_return("Bobby McFerrin")
      user.stub(:name => "Bobby McGee")
      user = mock(:user, :name => "Bobby McFerrin")
    
  • Chaining Stubs Sometimes you need to stub deep (watch out for this, it's a bad code smell). stub_chain can help:

      user.stub_chain(:account, :billing_address).and_return("717 California St.")
      user.account.billing_address # 717 California St.
    
  • Expectations A method stub that will raise an error if it is never called.

    user.should_receive(:name).and_return("Bobby McFerrin")

FAQ

Can you TDD when practicing BDD?

Sure. You'll have to circles of Red/Green development. The outer circle, (BDD) and the inner circle (unit testing).

Benefits of TDD

  • Smaller (shorter) development cycle. Focus on developing and delivering the smallest unit of value (just like your commits!)
  • Avoid over engineering by writing the simplest code that passes the test when practicing TDD. This discourages you from including rainy day code you might never use.
  • Develop the interface free of any implementation

Running Thangs

Default rspec output is lame. Make it pretty

rspec --color --format documentation some_spec.rb

Better yet, save this to your ~/.rspec config file:

echo "--color --format documentation" > ~/.rspec

RSpec with Rails

Add rspec-rails to your Gemfile, bundle install then rails generate rspec:install

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment