Skip to content

Instantly share code, notes, and snippets.

@msurovcak
Created November 11, 2015 01:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msurovcak/8a28cac38389abfebc2f to your computer and use it in GitHub Desktop.
Save msurovcak/8a28cac38389abfebc2f to your computer and use it in GitHub Desktop.
be_in matcher

simple be_in matcher

Works like a charm, with only simple "string is in array of strings":

$ rspec be_in.rb --format=documentation --color

passing examples
  bar
    should be in "foo" and "bar"
    should not be in "foo" and "baz"

failing examples
  baz
    should be in "foo" and "bar" (FAILED - 1)
    should not be in "foo" and "baz" (FAILED - 2)

Failures:

  1) failing examples baz should be in "foo" and "bar"
     Failure/Error: it { should be_in %w(foo bar) }
       expected "baz" to be in "foo" and "bar"
     # ./be_in.rb:19:in `block (3 levels) in <top (required)>'

  2) failing examples baz should not be in "foo" and "baz"
     Failure/Error: it { should_not be_in %w(foo baz) }
       expected "baz" not to be in "foo" and "baz"
     # ./be_in.rb:20:in `block (3 levels) in <top (required)>'

Finished in 0.03009 seconds (files took 0.16068 seconds to load)
4 examples, 2 failures

Failed examples:

rspec ./be_in.rb:19 # failing examples baz should be in "foo" and "bar"
rspec ./be_in.rb:20 # failing examples baz should not be in "foo" and "baz"
require 'rspec/expectations'
require 'rspec'
RSpec::Matchers.define :be_in do |expected|
match do
expected.include?(actual)
end
end
describe 'passing examples' do
describe 'bar' do
it { should be_in %w(foo bar) }
it { should_not be_in %w(foo baz) }
end
end
describe 'failing examples' do
describe 'baz' do
it { should be_in %w(foo bar) }
it { should_not be_in %w(foo baz) }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment