Skip to content

Instantly share code, notes, and snippets.

@brysgo
Last active August 29, 2015 14:12
Show Gist options
  • Save brysgo/abfb8c2ba5df8de33184 to your computer and use it in GitHub Desktop.
Save brysgo/abfb8c2ba5df8de33184 to your computer and use it in GitHub Desktop.
Linearly dependant tests in rspec
module BehaviorDSL
def behavior(name, &block)
metadata[:description_args].push(name)
refresh_description
yield
metadata[:description_args].pop
refresh_description
end
private
def refresh_description
metadata[:description] = metadata[:description_args].join(" ")
metadata[:full_description] = [metadata[:example_group][:full_description]].concat(metadata[:description_args]).join(" ")
end
def metadata
RSpec.current_example.metadata
end
end
RSpec.configure do |config|
config.include BehaviorDSL
end
describe Hash do
specify do
@hash = Hash.new
behavior "should set new keys properly" do
@hash[:foo] = 'bar'
@hash[:foo].should == 'bar'
end
behavior "overwrite old values with the same key" do
@hash[:foo] = 'baz'
@hash[:foo].should == 'baz'
end
end
end
describe Hash do
before do
@hash = Hash.new
end
it "should set new keys properly" do
@hash[:foo] = 'bar'
@hash[:foo].should == 'bar'
end
it "overwrite old values with the same key" do
@hash[:foo] = 'bar'
@hash[:foo].should == 'bar'
@hash[:foo] = 'baz'
@hash[:foo].should == 'baz'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment