Rspec comes with the flexibility of creating custom matcher as per your programme context.
To create such matcher you have to create matcher.rb
file (name it as you want) in spec/support
directory and put there your matcher definition:
Rspec::Matchers.define :custom_matcher do |expected|
match do |actual|
# return true or false
end
End
for example, we create a rspec matcher called be_json_success
Rspec::Matchers.define :be_json_success do |expected|
match do |actual|
json_response = JSON.parse(actual.body)
expect(json_response['success']).to eq(true)
end
end
The last step is to require your matcher by including require support/matchers
in the spec_helper.rb
file.
# spec_helper.rb
require 'support/matcher'
.
.
.
# use in http_test_spec.rb
require 'spec_helper'
describe 'get a call for API' do
it { should be_json_success }
end