Explain. Net unit test method in detail

Time:2019-11-27

1. Abnormal test

It can be used to test method exceptions directly or to test simulation objects. However, it is rarely used to test simulation objects. Therefore, this paper introduces the method exception test. Please see the following code. When the user name is empty, an exception will be thrown.

for example


 
public bool Valid(string userName, string passWord)
 
  {
 
    if (string.IsNullOrEmpty(userName)) throw new ArgumentNullException("userName is null");
 
    var isValid = userName == "admin" && passWord == "123456";
 
    Log.Write(userName);
 
    return isValid;
 
  }

The test code is as follows


 
[Test]
 
 [ExpectedException(typeof(ArgumentNullException))]
 public void Vaild_Throw_Test()
 {
   MyLogin l = new MyLogin();
   l.Valid("", "123456");
 }

2. Test return value

A class of lastcall is used here, which is quite common. Some auxiliary testing functions are in this class.

The test code is as follows


[Test]
public void Valid_Return()
{
  MockRepository mock = new MockRepository();
  var log = mock.DynamicMock<ILog>();
  using (mock.Record())
  {
    log.WriteLog("admin");
    LastCall.Return(0);
  }
  var returnValue = log.WriteLog("admin");
  Assert.AreEqual(returnValue, 0);
}   

Recommended Today

Manjaro uses SS method (requires nodejs environment)

Just installed manjaro, need to installshadowsocks-qt5+Proxy SwitchyOmega, but the latest Chrome has long blocked the installation of non Google store CRX channels. Here is my solution, which is troublesome but usable. If you are familiar with NPM commands in nodejs, you won’t find it troublesome, because nodejs is used for compilation. Preparation environment: A VPS […]