c# - Asserting with OR condition

青春壹個敷衍的年華 提交于 2019-12-07 03:13:01

问题


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


回答1:


Assert.IsTrue((myString.Substring(3,3) == "DEF" || myString.Substring(3,3) == "RED")?true:false,"Failed");



回答2:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!