Fork me on GitHub

UnitTest


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

 

posted @ 2014-07-01 22:13  种花生的读书人  阅读(469)  评论(3编辑  收藏  举报

该博客仅作为记录笔记,转载随意