首页 > 代码库 > UnitTest

UnitTest

#region File Header & Copyright Notice/* * Copyright (C) 2014 XXX, INC. All Rights Reserved. * THIS SOURCE CODE IS CONFIDENTIAL AND PROPRIETARY AND MAY NOT BE USED * OR DISTRIBUTED WITHOUT THE WRITTEN PERMISSION OF XXX, INC. */#endregionusing Bll;using Model;using Dal;using NUnit.Framework;using NUnit.Mocks;using System.ServiceModel;using Constant;namespace OES.Nunit{    /// <summary>    /// This class is used for test use business logic layer.    /// </summary>    [TestFixture]    public class UserNumit    {        private DynamicMock mock = null;        private IUserDal userDal = null;        private IUserBll userBll = null;        private User user = null;        [SetUp]        public void Initialize()        {            user = new User();            user.UserName = "a1";            user.Password = "1";            user.RoleType = "student";        }        [Test]        public void LogOn()        {            mock = new DynamicMock(typeof(IUserDal));            userDal = (IUserDal)mock.MockInstance;            userBll = new UserBll();            userBll = new UserBll(userDal);            mock.ExpectAndReturn("RetrieveUserByUserName", user, user.UserName);            User getUser = userBll.RetrieveUserByUserName(user.UserName);            Assert.IsNotNull(getUser, "User is not null");        }        [Test]        public void LogOnException()        {            mock = new DynamicMock(typeof(IUserDal));            userDal = (IUserDal)mock.MockInstance;            userBll = new UserBll();            userBll = new UserBll(userDal);            mock.ExpectAndThrow("RetrieveUserByUserName", new UserException(), user.UserName);            Assert.Throws<FaultException<MyExceptionContainer>>(                () => this.userBll.RetrieveUserByUserName(user.UserName)                );        }        [Test]        public void ChangePassword()        {            mock = new DynamicMock(typeof(IUserDal));            userDal = (IUserDal)mock.MockInstance;            userBll = new UserBll();            userBll = new UserBll(userDal);            string[] str = new string[] { "1", "123" };            mock.ExpectAndReturn("UpdatePassword", 1, str);            bool isChangePassword = userBll.UpdatePassword(str[0], str[1]);            Assert.True(isChangePassword, "Changed password");        }        [Test]        public void ChangePasswordException()        {            mock = new DynamicMock(typeof(IUserDal));            userDal = (IUserDal)mock.MockInstance;            userBll = new UserBll();            userBll = new UserBll(userDal);            string[] str = new string[] { "1", "123" };            mock.ExpectAndThrow("UpdatePassword", new UserException(), str);            Assert.Throws<FaultException<MyExceptionContainer>>(                () => this.userBll.UpdatePassword(str[0],str[1])                );        }        [TestFixtureTearDown]        ~UserNumit()        {            userBll = null;            user = null;            userDal = null;        }    }}