Search code examples
rubytestingminitestassertions

Is `assert_equal ... or assert_equal ...` possible in ruby minitest?


I am creating a test that can be expressed as a disjunction of assertions; when the first assertion fails, then it should look at the next. Particularly, an item will be equal to one of two things. Which one, I do not know.

My code looks something like this. It does not work but it might give you an idea of what I am trying to do.

asset_one = Cache.asset_one
asset_two = Cache.asset_two

assert_equal(asset_one.name, Pages.name) or 
assert_equal(asset_two.name, Pages.name)

The Pages.name should match one of the targets. I don't know which one. If it doesn't match the first, then I want it to skip it and try to match the second.

Any help is as always much appreciated.


Solution

  • If you want to match any one of the object having the given name, you can use assert_includes

    asset_one = Cache.asset_one
    asset_two = Cache.asset_two
    
    assert_includes([asset_one, asset_two].map(&:name), Pages.name)
    

    This should solve your purpose.