Created
September 21, 2011 21:26
-
-
Save jnicklas/1233365 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Steps are defined through simple text matches, I guess they | |
# could be regexen as well, not sure. Strings do look way nicer. | |
# placeholders are defined with a colon, like Ruby symbols, or | |
# Rails' routes. If a placeholder is not defined, it matches | |
# [\w]+. The placeholder is accessible inside the step by calling | |
# a method with the same name. | |
step "there are :count people" do | |
count.times { Person.make! } | |
end | |
step ":person has :count posts" do | |
count.times { person.posts.make! } | |
end | |
step ":person has :post" do | |
post(:person => person) | |
end | |
# It's possible to define a placeholder to match several possible expressions. | |
# These matching expressions can also use placeholders (yo dawg!). Placeholders | |
# are available through a method with the same name. The method `options` | |
# returns all placeholders and their values as a hash (e.g. :name => 'Jonas' | |
# below). It also contains additional options sent in when the placeholder is | |
# "materialized". In the example above, where `post(:person => person)` is | |
# called, the options hash might contain | |
# `:name => 'Jonas', :person => # <#Person>` | |
define :person do | |
match 'a person named ":name"' do | |
Person.make!(options) | |
end | |
match 'a person with email ":email"' do | |
Person.make!(options) | |
end | |
match '":name"' do | |
Person.find_by_name!(name) | |
end | |
end | |
define :post do | |
match 'a post titled ":title"' do | |
Post.make!(options) | |
end | |
match 'the post ":title"' do | |
Post.find_by_name!(name) | |
end | |
end | |
define :count do | |
match /\d+/ do |count| | |
count.to_i | |
end | |
end | |
__END__ | |
Example Usage of steps: | |
Given a person named "Jonas" has 3 posts | |
Given "Jonas" has 3 posts | |
Given a person with email "jonas.nicklas@gmail.com" has 3 posts | |
Given "Jonas" has a post titled "Something" | |
Given a person named "Jonas" has a post titled "Something" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wow sounds nice!