Created
February 22, 2012 18:51
-
-
Save maxim/1886617 to your computer and use it in GitHub Desktop.
Drier integration testing with RSpec and Capybara
This file contains hidden or 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
| # 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