13

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....

4

3 回答 3

9
Assert.IsTrue((myString.Substring(3,3) == "DEF" || myString.Substring(3,3) == "RED")?true:false,"Failed");
于 2013-06-28T07:19:14.760 回答
6

使用 NUnit,AnyOf 约束有效:

Assert.That(myString.Substring(3,3), Is.AnyOf("DEF", "RED"));
于 2021-06-16T13:40:09.380 回答
3

根据您使用的单元测试框架,您可以执行以下操作:

Assert.Contains(myString.Substring(3, 3), new [] { "DEF", "RED" });

但请注意,这有点滥用系统,因为它切换了预期实际

应该与任何框架一起使用并且不会滥用系统的替代方法如下所示:

Assert.True(new [] { "DEF", "RED" }.Contains(myString.Substring(3, 3)));
于 2013-06-28T07:23:46.570 回答