. Net unit test-common testing methods (exception simulation, return value testing, parameter testing, and database access code testing)

Source: Internet
Author: User

     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;        }

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

        [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);        }   

 

    public bool Valid_Paramter(string userName, string passWord)        {            Log.Write(Guid.NewGuid() + userName);            return userName == "admin" && passWord == "123456";        }

[Test] public void Valid_Paramter () {MockRepository mock = new MockRepository (); var log = mock. dynamicMock <ILog> (); using (mock. record () {log. write ("admin"); LastCall. constraints (Rhino. mocks. constraints. text. contains ("admin");} // if the code here is not clear, you can refer to the previous section, which provides instructions. MyLogin login = new MyLogin (); login. log = log; var valid = login. valid_Paramter ("admin"," 123456 "); Assert. areEqual (valid, true); mock. verifyAll ();}

 

  public class RollbackAttribute : Attribute, ITestAction    {        private TransactionScope transaction;        public void BeforeTest(TestDetails testDetails)        {            transaction = new TransactionScope();        }        public void AfterTest(TestDetails testDetails)        {            transaction.Dispose();        }        public ActionTargets Targets        {            get { return ActionTargets.Test; }        }    }

  

   public int Insert(string title)        {            testEntities db = new testEntities();            var titleInfo = new TitleInfo()            {                Title = title            };            db.TitleInfo.Add(titleInfo);            return db.SaveChanges();        }

 

        [Test]        [Rollback]        public void Test_Insert()        {            DBLibrary db = new DBLibrary();            var count = db.Insert("admin");            Assert.True(count > 0);        }  

  

 

 

    [TestFixture]    class DBLibraryTest    {        TransactionScope socp;        [SetUp]        public void Init()        {            socp = new TransactionScope();        }        [TearDown]        public void Close()        {            socp.Dispose();        }        [Test]        public void Test_Insert()        {            DBLibrary db = new DBLibrary();            var count = db.Insert("admin");            Assert.True(count > 0);        }    }

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.