r7kamura / rspec-json_matcher

RSpec matcher for testing JSON string
MIT License
171 stars 10 forks source link

Alias AbstractMatcher#matches? as === #7

Closed hanazuki closed 7 years ago

hanazuki commented 7 years ago

Defining === method on a matcher allows us to use the matcher in

Use cases:

# Composing matchers
expect({'data' => {'a' => 1}.to_json}).to match('data' => be_json)
expect([{'a' => 1}.to_json, {'b' => 2}.to_json]).to all(be_json)

# Matching a nested JSON
expect({'a' => {'b' => 1}.to_json}.to_json).to be_json_as('a' => be_json)

# Matching method arguments with rspec-mocks
object = double
expect(object).to receive(:some_method).with(be_json_as('a' => 1))
object.some_method({'a' => 1}.to_json)

Users can alias the matchers as, for example, a_json if they like a more natual wording:

RSpec::Matchers.alias_matcher :a_json, :be_json
...
expect(object).to receive(:some_method).with(a_json_as('a' => 1))
r7kamura commented 7 years ago

@hanazuki Looks so great!

hanazuki commented 7 years ago

Thank you.