RSpec Situations

RSpec DSL for creating situations and combining them for testing  

https://github.com/brett-richardson/rspec-situations

Adds a super simple method to describe RSpec situations in terms of smaller situation blocks.

Define a situation with the ‘situation’ method. Add a symbol key to identify the situation, and add an optional description if you like, and pass in a block creating the situation.

Using Rspec Situations

Using Rspec Situations is super simple.

In your Gemfile:

group :development, :testing do
  gem 'rspec-situations'
end

In your spec_helper.rb:

require 'rspec/situations'

Define a situation with the ‘situation’ method. Add a symbol key to identify the situation, and add an optional description if you like, and pass in a block creating the situation.

Describe a set of 1 or more situations with the ‘describe_situation’ method, and treat it just like a normal describe call.

describe Apple do
  subject(:apple){ create :apple }

  situation(:bought) { subject.bought_by = create :user }
  situation(:red) { subject.color = :red }

  situation(:one_yr_old, 'a year old') do # With optional description
    subject.created_at = 1.years.ago
  end

  describe_situation(:bought, :red) do
    it { should be_tasty }
  end

  desribe_situation(:bought, :one_yr_old, 'bought a year ago') do # With optional description
    it { should_not be_tasty }
  end

  describe_situation(:bought, :one_yr_old) do
    it { should_not be_tasty }
  end
end
comments powered by Disqus