Skip to content

Instantly share code, notes, and snippets.

@lazyatom
Created January 2, 2011 11:53
Show Gist options
  • Save lazyatom/762468 to your computer and use it in GitHub Desktop.
Save lazyatom/762468 to your computer and use it in GitHub Desktop.
Another approach
context "Posting" do
# this is what we are testing, and is run for each test
action do
post "/#{@stream}/messages?type=#{@type}&oauth_token=#{@token}", @payload
end
# this is basically setup, but describing the values as defaults clarifies their
# relationship to the action above. each further setup or test can modify these values
# before the action is actually invoked.
defaults do
@account = new_account
@stream = "test"
@type = "blah"
@payload = {:content => "hello"}.to_json
@token = "invalid"
end
# the next three methods are "partial setups"
def nothing
end
def a_valid_token
@token = token_for(@account)
end
def permission_for_that_stream
authorize @account, @stream
end
# each `with` defines a new context, where the setup is constructed based on the
# symbols passed. In this case, the `nothing` method is purely for aesthetics
with :nothing do
should "not post the message" do
end
should "respond with an unauthenticated error" do
end
end
with :a_valid_token do
should "not post the message" do
end
should "respond with an unauthorized error"
end
with :permission_for_that_stream do
should "not post the message"
should "respond with an unauthenticated error"
end
with :a_valid_token, :permission_for_that_stream do
should "post the message" do
end
should "respond with the message"
should "respond with an accessible access control header"
should "respect the type of the post"
should "distribute the message to connected clients"
end
end
module With
def defaults(*args, &block)
setup(*args, &block)
end
def action(&block)
define_method(:action, &block)
end
def with(*setups, &block)
name = "with " + setups.map { |s| de_methodize(s) }.join(" and ")
c = context(name, &block)
c.setup do
setups.each { |s| send(s) }
send(:action)
end
c
end
end
Kintama.extend With
def new_account
nil
end
def token_for(account)
"valid-generated-token"
end
def authorize(account, stream)
puts "authorizing #{stream} for #{account}"
end
def post(url, payload)
puts "POSTING to #{url} with #{payload.inspect}"
end
@floehopper
Copy link

I like the explicitness of the named "partial setups", but dislike the implicitness of the "action" and the "default". I like it that you can avoid multi-level nesting by combining multiple "partial setups" in one "with". I hope that helps.

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