Skip to content

Instantly share code, notes, and snippets.

@maxim
Created February 22, 2012 18:51
Show Gist options
  • Select an option

  • Save maxim/1886617 to your computer and use it in GitHub Desktop.

Select an option

Save maxim/1886617 to your computer and use it in GitHub Desktop.
Drier integration testing with RSpec and Capybara
# Drier integration testing with RSpec and Capybara
# Not happy with this
page.should have_content("foo")
page.should have_content("bar")
page.should have_unchecked_field("baz")
# Better
page_should do
have_content("foo")
have_content("bar")
have_unchecked_field("baz")
end
# Implementation
def page_should(&block)
outer_context = self
context = Object.new
context.class_eval do
define_method :page do
outer_context.page
end
define_method :method_missing do |meth, *args, &block|
obj = outer_context.send(meth, *args, &block)
if obj.respond_to?(:matches?)
page.should(obj)
else
obj
end
end
end
context.instance_eval(&block)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment