码迷,mamicode.com
首页 > 其他好文 > 详细

UnitTest

时间:2014-07-02 10:07:59      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   2014   for   io   

#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.
 */
#endregion

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

 

UnitTest,布布扣,bubuko.com

UnitTest

标签:style   blog   color   2014   for   io   

原文地址:http://www.cnblogs.com/chenyongblog/p/3819146.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!