i am checking a string for three characters
Assert.AreEqual(myString.Substring(3,3), "DEF", "Failed as DEF was not observed");
the thing is here it can be DEF
or RES
, now to handle this what i can think of is the following
bool check = false;
if( myString.Substring(3,3) == "DEF" || myString.Substring(3,3) == "RED" )
check = true;
Assert.IsTrue(check,"Failed");
Console.WriteLine(""Passed);
IS THERE a way i can use some OR thing within Assert
p.s i'm writing unit test & yes i will use ternary operator instead....
Assert.IsTrue((myString.Substring(3,3) == "DEF" || myString.Substring(3,3) == "RED")?true:false,"Failed");
Depending on the unit testing framework you are using you could do something like this:
Assert.Contains(myString.Substring(3, 3), new [] { "DEF", "RED" });
But beware, this is abusing the system a bit, because it switched expected and actual.
An alternative approach that should work with any framework and doesn't abuse the system would look like this:
Assert.True(new [] { "DEF", "RED" }.Contains(myString.Substring(3, 3)));
来源:https://stackoverflow.com/questions/17359284/c-sharp-asserting-with-or-condition