问题
In C#, I have a method which throws an exception when a certain condition is true. I am supposed to write a unit test method to verify that.
Since the method being tested doesn't return a boolean value, I can't use Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsTrue
method. Which assert method can I use? Thanks.
回答1:
Use Assert.Throws
, like so:
var ex = Assert.Throws<ArgumentNullException>(() => My.Method(null));
Assert.Equals("foo", ex.ParamName);
This works in XUnit and in the later versions of MSTest (install MSTest as a NuGet package to get up-to-date versions)
来源:https://stackoverflow.com/questions/44316606/is-there-some-assert-method-which-can-be-used-for-unit-test-on-whether-a-method