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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With