Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assert.AreEqual(expected, actual) for multiple expected values

How can I assert that a variable satisfies any one of two values in Visual Studio's unit testing environment?

I've seen other testing frameworks do it like this:

Assert.That(result.ViewName, Is.EqualTo("HomePage") | Is.Empty);

I'm not experienced with Visual Studio's unit testing environment.

like image 786
Petrus Theron Avatar asked Nov 22 '10 20:11

Petrus Theron


1 Answers

First of all, I think the sample line of code you typed contains an error, if you have no intention to perform a bitwise OR.

Assert.That(result.ViewName, Is.EqualTo("HomePage") || Is.Empty);

Secondly, I suspect a misleading test here, since unit testing shall tests for one and only one specific scenario. So, if you assigned, for example, one value to a property, you expect this very value to be returned by its getter.

Third, here's how I would go for it, considering I might miss your point and you definitely need to test for either result.

Assert.IsTrue(string.Equals(result.ViewName, "HomePage") || string.Equals(result.ViewName, string.Empty));

On the other hand, I have never ever tested a scenario where I expected two different results from the same unit test. Assert.That might be good as well.

like image 73
Will Marcouiller Avatar answered Nov 20 '22 20:11

Will Marcouiller