Search code examples
unit-testingscalaspecs2

Specs2: How to Test if "abc" contains all of "aabb"


Specs2 does provide a containsAllOf matcher but I can't get it to work the way I want. I have two Strings A and B, and I want to test that all Chars that occur in B are present in A.

My best try so far was

A.toCharArray.toSeq must containAllOf(B.toCharArray.toSeq)

But this fails with errors like

WrappedArray(a, b, c, d, ...) does not contain S, a, V, H, I, ... and must not contain ...

  1. The WrappedArray does contain the characters it allegedly doesn't according to Specs2
  2. Why is there a test for elements A must not contain? I don't want ot test equality, I want to test if B is a "subset" of A (but not in the strict set-theoretic definition of set)

How can I write a working spec for this scenario?


Solution

  • Currently, I've chosen this variant:

    B.toSeq.map(c => A must contain(c))
    

    I'm still open to suggestions, but if there is no cleaner solution I might (for the first time) answer my own question.

    Or is this one of the cases where I suspect Specs2 to test a thing, when it actually doesn't?