Skip to content

Instantly share code, notes, and snippets.

@chyld
Last active December 14, 2015 22:28
Show Gist options
  • Save chyld/5158329 to your computer and use it in GitHub Desktop.
Save chyld/5158329 to your computer and use it in GitHub Desktop.
***RSPEC***
Equivalence
actual.should eq(expected) # passes if actual == expected
actual.should == expected # passes if actual == expected
actual.should eql(expected) # passes if actual.eql?(expected)
Identity
actual.should be(expected) # passes if actual.equal?(expected)
actual.should equal(expected) # passes if actual.equal?(expected)
Comparisons
actual.should be > expected
actual.should be >= expected
actual.should be <= expected
actual.should be < expected
actual.should be_within(delta).of(expected)
Regular expressions
actual.should match(/expression/)
actual.should =~ /expression/
Types/classes
actual.should be_an_instance_of(expected)
actual.should be_a_kind_of(expected)
Truthiness
actual.should be_true # passes if actual is truthy (not nil or false)
actual.should be_false # passes if actual is falsy (nil or false)
actual.should be_nil # passes if actual is nil
Expecting errors
expect { ... }.to raise_error
expect { ... }.to raise_error(ErrorClass)
expect { ... }.to raise_error("message")
expect { ... }.to raise_error(ErrorClass, "message")
Expecting throws
expect { ... }.to throw_symbol
expect { ... }.to throw_symbol(:symbol)
expect { ... }.to throw_symbol(:symbol, 'value')
Yielding
expect { |b| 5.tap(&b) }.to yield_control # passes regardless of yielded args
expect { |b| yield_if_true(true, &b) }.to yield_with_no_args # passes only if no args are yielded
expect { |b| 5.tap(&b) }.to yield_with_args(5)
expect { |b| 5.tap(&b) }.to yield_with_args(Fixnum)
expect { |b| "a string".tap(&b) }.to yield_with_args(/str/)
expect { |b| [1, 2, 3].each(&b) }.to yield_successive_args(1, 2, 3)
expect { |b| { :a => 1, :b => 2 }.each(&b) }.to yield_successive_args([:a, 1], [:b, 2])
Predicate matchers
actual.should be_xxx # passes if actual.xxx?
actual.should have_xxx(:arg) # passes if actual.has_xxx?(:arg)
Ranges (Ruby >= 1.9 only)
(1..10).should cover(3)
Collection membership
actual.should include(expected)
actual.should start_with(expected)
actual.should end_with(expected)
Expect syntax
expect(actual).to eq expected
expect(actual).to be > 3
expect([1, 2, 3]).to_not include 4
***CAPYBARA***
Navigating
visit('/projects')
visit(post_comments_path(post))
current_path.should == post_comments_path(post)
Actions
- (Object) attach_file(locator, path)
- (Object) check(locator)
- (Object) choose(locator)
- (Object) click_button(locator)
- (Object) click_link(locator)
- (Object) click_link_or_button(locator) (also: #click_on)
- (Object) fill_in(locator, options = {})
- (Object) select(value, options = {})
- (Object) uncheck(locator)
- (Object) unselect(value, options = {})
Matchers
- (Object) ==(other)
- (Object) assert_no_selector(*args)
- (Object) assert_selector(*args)
- (Boolean) has_button?(locator)
- (Boolean) has_checked_field?(locator)
- (Boolean) has_css?(path, options = {})
- (Boolean) has_field?(locator, options = {})
- (Boolean) has_link?(locator, options = {})
- (Boolean) has_no_button?(locator)
- (Boolean) has_no_checked_field?(locator)
- (Boolean) has_no_css?(path, options = {})
- (Boolean) has_no_field?(locator, options = {})
- (Boolean) has_no_link?(locator, options = {})
- (Boolean) has_no_select?(locator, options = {})
- (Boolean) has_no_selector?(*args)
- (Boolean) has_no_table?(locator, options = {})
- (Boolean) has_no_text?(content) (also: #has_no_content?)
- (Boolean) has_no_unchecked_field?(locator)
- (Boolean) has_no_xpath?(path, options = {})
- (Boolean) has_select?(locator, options = {})
- (Boolean) has_selector?(*args)
- (Boolean) has_table?(locator, options = {})
- (Boolean) has_text?(content) (also: #has_content?)
- (Boolean) has_unchecked_field?(locator)
- (Boolean) has_xpath?(path, options = {})
Finders
- (Capybara::Result) all([kind], locator, options)
- (Capybara::Element) find(*args)
- (Capybara::Element) find_button(locator, options = {})
- (Capybara::Element) find_by_id(id, options = {})
- (Capybara::Element) find_field(locator, options = {}) (also: #field_labeled)
- (Capybara::Element) find_link(locator, options = {})
- (Capybara::Element) first([kind], locator, options)
Scoping
within("li#employee") do
fill_in 'Name', :with => 'Jimmy'
end
within(:xpath, "//li[@id='employee']") do
fill_in 'Name', :with => 'Jimmy'
end
Scripting
page.execute_script("$('body').empty()")
result = page.evaluate_script('4 + 4');
Debugging
save_and_open_page
print page.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment