Assert.Fail("Expected ArgumentNullException, but {1} was thrown instead.", e.GetType().Name); } }
public void demonstrateThatExceptionThrown() { string value; Dictionary<string, string> test = new Dictionary<string, string>(); Assert.Throws(typeof(KeyNotFoundException), () => value = test["h"]); } 执行时,您将在VisualStudio中收到一条警告,指出在用户代码中未处理异常。如果你看一下callst...
ICommand command = CreateCommandObj(); Assert.That( () => command.DoWork(), Throws.TypeOf<ArgumentException>() .And.Message.Equals("MyMessage")); This approach allows me to check properties on the thrown exception. I can add any number of And or Or's to the Assert.Thanks everyone f...
1. Assert.AreEqual(object expected, object actual[,string message]) verifies that two objects are equal if they are not equal, an NUnit.Framwork.AssertionException is thrown 参数说明: expected:期望值(通常是硬编码的) actual:被测试代码实际产生的值、 message:一个可选消息,将会在发生错误时报告这个...
catch(InsufficientFundsException expected) { } Assert.AreEqual(200.00F,source.Balance); Assert.AreEqual(150.00F,destination.Balance); } We are testing the transactional property of our business method – all operations are successful or none. Compile and run – red bar. OK, we’ve made $300.00...
public static Exception Throws(Action action) 1. { 1. return Throws(action, null); 1. } 1. /// 1. /// Executes an exception, expecting an exception to be thrown. 1. /// Like Assert.Throws in NUnit. 1. /// 1. /// The action...
public void Test() { throw new MyCustomException("You can't do that!"); } [TestMethod] public void ThisWillPassIfExceptionThrown() { var exception = Assert.ThrowsException<MyCustomException>( () => Test(), "This should have thrown!"); Assert.AreEqual("You can't do that!", excepti...
public static Exception Throws(Action action) { return Throws(action, null); } /// /// Executes an exception, expecting an exception to be thrown. /// Like Assert.Throws in NUnit. /// /// The action to execute /// The error message if the expected exception is...
Assert.Pass:使测试用例成功 TheAssert.Passmethod allows you to immediately end the test, recording it as successful. Since it causes an exception to be thrown, it is more efficient to simply allow the test to return. However, Assert.Pass allows you to record a message in the test result an...
字符串 Constraint类重载了!操作符,这在某种程度上相当于创建新的NotConstraint。例如,在这种情况下,...