dushyantp
dushyantp

Reputation: 4720

Assert string array member content

In this test, the required assertion is that if a member of the obtained string array has a value which is similar to something.

For example: Say the obtained string array is

string[] obtainedArr = new string("c:\programfiles", "d:\morefiles", "e:\some");

Do we have something in the lines of:

Assert.That(obtainedArr, Has.Member.Which.Is.String.Containing("d:"), "D: location not obtained");

Or do we have a better way to achieving this?

Upvotes: 2

Views: 815

Answers (1)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236228

Assert.True(obtainedArr.Any(s => s.Contains("d:")), "D: location not obtained");

Notes: by default string comparison is case-sensitive, so searching for D: will fail for your sample input. Also if you are looking for sub-path, then maybe StartsWith is more appropriate check, than Contains

Assert.That(obtainedArr.Any(s => 
   s.StartsWith("d:", StringComparison.InvariantCultureIgnoreCase));

Of course, this code is not very readable, so I would create (extension) method to check if given path is subpath of some other path.

Upvotes: 5

Related Questions