stack twitter tryhackme rss linkedin cross

Wilco van Esch

Skip to main content

Search results

    Using RSpec to verify a result matches one of multiple expectations

    Say you want to verify that a method returns either 'Yes' or 'Maybe'.

    You could of course do:

    valid = ["Yes", "Maybe"]
    expect(valid).to include(response)
    

    This turns the expectation around to check that the valid responses include the returned value.

    However, this is likely inconsistent with your other RSpec expectations and is not as semantically lucid as the following option.

    expect(response).to eq("Yes").or eq("Maybe")
    

    You can find out more about compound expectations in the official RSpec documentation.