Skip to content

Instantly share code, notes, and snippets.

@myronmarston
Created May 10, 2014 07:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myronmarston/931012eb70c002b87d44 to your computer and use it in GitHub Desktop.
Save myronmarston/931012eb70c002b87d44 to your computer and use it in GitHub Desktop.
Demonstration of using built in matchers from within a custom matcher
Run options: include {:focus=>true}
All examples were filtered out; ignoring {:focus=>true}
Using built in matchers from custom ones
fails when the word does not end with the given letter (FAILED - 1)
fails when the word does not start with the given letter (FAILED - 2)
can match properly
Failures:
1) Using built in matchers from custom ones fails when the word does not end with the given letter
Failure/Error: expect("poopin'").to start_and_end_with("p")
expected "poopin'" to start and end with "p"
# ./lib/rspec/expectations/fail_with.rb:33:in `fail_with'
# ./lib/rspec/expectations/handler.rb:38:in `handle_failure'
# ./lib/rspec/expectations/handler.rb:49:in `handle_matcher'
# ./lib/rspec/expectations/expectation_target.rb:54:in `to'
# ./spec/foo_spec.rb:18:in `block (2 levels) in <top (required)>'
# /Users/myron/code/rspec-dev/repos/rspec-core/lib/rspec/core/example.rb:148:in `instance_exec'
# /Users/myron/code/rspec-dev/repos/rspec-core/lib/rspec/core/example.rb:148:in `block in run'
# /Users/myron/code/rspec-dev/repos/rspec-core/lib/rspec/core/example.rb:299:in `with_around_example_hooks'
# /Users/myron/code/rspec-dev/repos/rspec-core/lib/rspec/core/example.rb:145:in `run'
# /Users/myron/code/rspec-dev/repos/rspec-core/lib/rspec/core/example_group.rb:494:in `block in run_examples'
# /Users/myron/code/rspec-dev/repos/rspec-core/lib/rspec/core/example_group.rb:490:in `map'
# /Users/myron/code/rspec-dev/repos/rspec-core/lib/rspec/core/example_group.rb:490:in `run_examples'
# /Users/myron/code/rspec-dev/repos/rspec-core/lib/rspec/core/example_group.rb:457:in `run'
# /Users/myron/code/rspec-dev/repos/rspec-core/lib/rspec/core/runner.rb:112:in `block (2 levels) in run_specs'
# /Users/myron/code/rspec-dev/repos/rspec-core/lib/rspec/core/runner.rb:112:in `map'
# /Users/myron/code/rspec-dev/repos/rspec-core/lib/rspec/core/runner.rb:112:in `block in run_specs'
# /Users/myron/code/rspec-dev/repos/rspec-core/lib/rspec/core/reporter.rb:49:in `report'
# /Users/myron/code/rspec-dev/repos/rspec-core/lib/rspec/core/runner.rb:108:in `run_specs'
# /Users/myron/code/rspec-dev/repos/rspec-core/lib/rspec/core/runner.rb:86:in `run'
# /Users/myron/code/rspec-dev/repos/rspec-core/lib/rspec/core/runner.rb:70:in `run'
# /Users/myron/code/rspec-dev/repos/rspec-core/lib/rspec/core/runner.rb:38:in `invoke'
# /Users/myron/code/rspec-dev/repos/rspec-core/exe/rspec:4:in `<top (required)>'
# ./bin/rspec:12:in `load'
# ./bin/rspec:12:in `<main>'
2) Using built in matchers from custom ones fails when the word does not start with the given letter
Failure/Error: expect("apoop").to start_and_end_with("p")
expected "apoop" to start and end with "p"
# ./lib/rspec/expectations/fail_with.rb:33:in `fail_with'
# ./lib/rspec/expectations/handler.rb:38:in `handle_failure'
# ./lib/rspec/expectations/handler.rb:49:in `handle_matcher'
# ./lib/rspec/expectations/expectation_target.rb:54:in `to'
# ./spec/foo_spec.rb:14:in `block (2 levels) in <top (required)>'
# /Users/myron/code/rspec-dev/repos/rspec-core/lib/rspec/core/example.rb:148:in `instance_exec'
# /Users/myron/code/rspec-dev/repos/rspec-core/lib/rspec/core/example.rb:148:in `block in run'
# /Users/myron/code/rspec-dev/repos/rspec-core/lib/rspec/core/example.rb:299:in `with_around_example_hooks'
# /Users/myron/code/rspec-dev/repos/rspec-core/lib/rspec/core/example.rb:145:in `run'
# /Users/myron/code/rspec-dev/repos/rspec-core/lib/rspec/core/example_group.rb:494:in `block in run_examples'
# /Users/myron/code/rspec-dev/repos/rspec-core/lib/rspec/core/example_group.rb:490:in `map'
# /Users/myron/code/rspec-dev/repos/rspec-core/lib/rspec/core/example_group.rb:490:in `run_examples'
# /Users/myron/code/rspec-dev/repos/rspec-core/lib/rspec/core/example_group.rb:457:in `run'
# /Users/myron/code/rspec-dev/repos/rspec-core/lib/rspec/core/runner.rb:112:in `block (2 levels) in run_specs'
# /Users/myron/code/rspec-dev/repos/rspec-core/lib/rspec/core/runner.rb:112:in `map'
# /Users/myron/code/rspec-dev/repos/rspec-core/lib/rspec/core/runner.rb:112:in `block in run_specs'
# /Users/myron/code/rspec-dev/repos/rspec-core/lib/rspec/core/reporter.rb:49:in `report'
# /Users/myron/code/rspec-dev/repos/rspec-core/lib/rspec/core/runner.rb:108:in `run_specs'
# /Users/myron/code/rspec-dev/repos/rspec-core/lib/rspec/core/runner.rb:86:in `run'
# /Users/myron/code/rspec-dev/repos/rspec-core/lib/rspec/core/runner.rb:70:in `run'
# /Users/myron/code/rspec-dev/repos/rspec-core/lib/rspec/core/runner.rb:38:in `invoke'
# /Users/myron/code/rspec-dev/repos/rspec-core/exe/rspec:4:in `<top (required)>'
# ./bin/rspec:12:in `load'
# ./bin/rspec:12:in `<main>'
Finished in 0.00146 seconds (files took 0.09554 seconds to load)
3 examples, 2 failures
Failed examples:
rspec ./spec/start_and_end_with_spec.rb:17 # Using built in matchers from custom ones fails when the word does not end with the given letter
rspec ./spec/start_and_end_with_spec.rb:13 # Using built in matchers from custom ones fails when the word does not start with the given letter
Randomized with seed 47699
describe "Using built in matchers from custom ones" do
RSpec::Matchers.define :start_and_end_with do |letter|
match do |string|
expect(string).to start_with(letter)
expect(string).to end_with(letter)
end
end
it 'can match properly' do
expect("poop").to start_and_end_with("p")
end
it 'fails when the word does not start with the given letter' do
expect("apoop").to start_and_end_with("p")
end
it 'fails when the word does not end with the given letter' do
expect("poopin'").to start_and_end_with("p")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment