Created
February 24, 2022 08:40
-
-
Save elct9620/8ce44e6aa458362d473e080365175b7e to your computer and use it in GitHub Desktop.
RSpec have_attributes example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# API | |
class FakeResponse < Struct.new(:status, :code) | |
def success? | |
code == 200 | |
end | |
end | |
class FakeAPI | |
def call | |
FakeResponse.new('Success', 200) | |
end | |
end | |
# Test | |
RSpec.describe FakeAPI do | |
subject(:api) { FakeAPI.new } | |
describe '#call' do | |
subject { api.call } | |
it { is_expected.to be_success } | |
it { is_expected.to have_attributes(status: match(/Success/)) } | |
it { is_expected.to have_attributes(code: 200) } | |
end | |
end | |
# Generated `--format document` | |
# FakeAPI | |
# #call | |
# is expected to be success | |
# is expected to have attributes {status: match(/Success/)} | |
# is expected to have attributes {code: 200 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To test a "nested" result, use
describe
but not suggested more than 3 levels.