locked
Unit Test in 2.0 .Net Framework RRS feed

  • Question

  • Is it possible to write unit tests for the 2.0 .NET  framework application.How can we test as aspx page is tightly coupled  with UI and we used so many user controls.If possible please suggest the best method.


    lucky


    • Edited by lucky.09 Tuesday, December 29, 2015 4:58 AM
    • Moved by Kristin Xie Wednesday, December 30, 2015 2:40 AM move to better forum
    Tuesday, December 29, 2015 4:56 AM

All replies

  • You would unit test the middle man, the MVP Presenter, because the Presenter uses an Interface a contract between the aspx.cs code behind for the page tied to its Presenter. Also user controls would have their own Presenter that can be unit tested.

    https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93presenter

    https://msdn.microsoft.com/en-us/library/ff650441.aspx

    http://polymorphicpodcast.com/shows/mv-patterns/

    using System;
    
    namespace DemoWebApp.Views
    {
        public interface IView
        {
            Boolean IsPostback { get; set; }
        }
    }
    
    using System.Web.UI.WebControls;
    using BLL.DTO;
    using System.Collections.Generic;
    
    namespace DemoWebApp.Views
    {
        public interface IPayRollView :IView
        {
            List<DTOAuthor> DtoAuthors { get; set; }
            DropDownList DrpAuthors { get; set; }
            Label LblPayRollId { get; set; }
            TextBox TbxSalary { get; set; }
        }
    }
    
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using DemoWebApp.Presenters;
    using DemoWebApp.Views;
    using BLL.DTO;
    using Services;
    
    namespace DemoWebApp
    {
        public partial class PayRollView : Page, IPayRollView
        {
            #region Members
            private readonly PayRollViewPresenter mPresenter;
            #endregion
    
            #region Properties
            
            public List<DTOAuthor> DtoAuthors
            {
                get { return (List<DTOAuthor>) Session["AuthorsObj"] ;}
                set { Session["AuthorsObj"] = value; }
            }
    
            public bool IsPostback { get; set; }
    
            public DropDownList DrpAuthors
            {
                get { return DRPAuthors; }
                set { DRPAuthors = value; }
            }
    
            public Label LblPayRollId
            {
                get { return LBLPayrollID; }
                set { LBLPayrollID = value; }
            }
    
            public TextBox TbxSalary
            {
                get { return TBXSalary; }
                set { TBXSalary  = value; }
            }
            #endregion
    
            #region Constructors
    
            public PayRollView()
            {
                mPresenter = new PayRollViewPresenter(this, TheService1.Instance);
            }
            #endregion
    
            protected void Page_Load(object sender, EventArgs e)
            {
                IsPostBack  = Page.IsPostBack;
                mPresenter.PageLoad();
            }
    
            protected void DRPAuthors_SelectedIndexChanged(object  sender, EventArgs e)
            {
                mPresenter.DRPAuthors_SelectedIndexChanged();  
            }
    
            protected void btnUpdate_Click(object sender, EventArgs e)
            {
                mPresenter.btnUpdateClick();
            }
    
            protected void btnAdd_Click(object sender, EventArgs e)
            {
                mPresenter.btnAddClick();
            }
    
            protected void btnDelete_Click(object sender, EventArgs e)
            {
                mPresenter.btnDeleteClick();
            }
        }
    }
    
    using System;
    using System.Collections.Generic;
    using DemoWebApp.Views;
    using BLL.DTO;
    using Services.IServices;
    
    namespace DemoWebApp.Presenters
    {
        public class PayRollViewPresenter
        {
            private readonly IPayRollView mView;
            private readonly IService1 mService;
    
            #region constructor
    
            public PayRollViewPresenter(IPayRollView view, IService1  theService)
            {
                mView = view;
                mService = theService;
            }
    
            #endregion
    
            #region Public Methods
    
            public void PageLoad()
            {
                int id = 0;
    
                if (!mView.IsPostback)
                {
                    var authors = mService.GetAuthors();
                    PopulateDRPAuthors(authors);
                    mView.DtoAuthors = authors;
    
                    if (authors.Count > 0)
                        id = authors[0].AuthorID;
                   
                    if (id > 0)
                        PopulateFields(mService.GetPayRollByAuthorID(id));
                }
            }
    
            public void DRPAuthors_SelectedIndexChanged()
            {
                int idx = mView.DrpAuthors.SelectedIndex;
               
                if (mView.DrpAuthors.Items.Count > 0)
                {
                    PopulateFields(mService.GetPayRollByAuthorID(mView.DtoAuthors[idx].AuthorID));
                }
            }
           
            public void btnUpdateClick()
            {
                int idx = mView.DrpAuthors.SelectedIndex;
    
                var payroll = mService.GetPayRollByAuthorID(mView.DtoAuthors[idx].AuthorID);
    
                if (payroll.PayRollID > 0 
                    && mView.TbxSalary.Text != string.Empty)
                {
                    payroll.Salary = Convert.ToInt32(mView.TbxSalary.Text);
    
                    mService.UpdatePayRollSalary(payroll);
                }
            }
    
            public void btnAddClick()
            {
                var payroll = new DTOPayroll();
                     
                int idx = mView.DrpAuthors.SelectedIndex;
    
                var author = mView.DtoAuthors[idx];
    
                payroll.AuthorID = author.AuthorID;
    
                if (mView.TbxSalary.Text != string.Empty
                    && Convert.ToInt32(mView.LblPayRollId.Text) == 0)
                {
                    if (Convert.ToInt32(mView.TbxSalary.Text) > 0)
                    {
                        payroll.Salary = Convert.ToInt32(mView.TbxSalary.Text);
                        PopulateFields(mService.AddPayRoll(payroll));
                    }
                }
            }
    
            public void btnDeleteClick()
            {
                int idx = mView.DrpAuthors.SelectedIndex;
    
                var payroll = mService.GetPayRollByAuthorID(mView.DtoAuthors[idx].AuthorID);
    
                if (Convert.ToInt32(mView.LblPayRollId.Text) > 0
                    && payroll.PayRollID > 0)
                {
                    mService.DeletePayRoll(payroll);
                    PopulateFields(mService.GetPayRollByAuthorID(mView.DtoAuthors[idx].AuthorID));
                }
              }
      
            #endregion
    
            #region Private Methods
    
            private void PopulateDRPAuthors(List<DTOAuthor> authors)
            {
                if (authors.Count > 0)
                {
                    mView.DrpAuthors.DataSource = authors;
                    mView.DrpAuthors.DataTextField = "FirstName";
                    mView.DrpAuthors.DataValueField = "AuthorID";
                    mView.DrpAuthors.DataBind();
                }
            }
    
            private void PopulateFields(DTOPayroll payroll)
            {
                if (payroll != null)
                {
                    mView.LblPayRollId.Text = payroll.PayRollID.ToString();
                    mView.TbxSalary.Text = payroll.Salary.ToString();
                }
            }
    
            #endregion
        }
    } 
    
    using System.Collections.Generic;
    using System.Web.UI.WebControls;
    using Moq;
    using NUnit.Framework;
    using Services.IServices;
    using BLL.DTO;
    using IPayRollView = DemoWebApp.Views.IPayRollView;
    using PayRollViewPresenter = DemoWebApp.Presenters.PayRollViewPresenter;
    
    namespace UnitTests.Web
    {
        [TestFixture]
        public class PayRollViewPresenterTest
        {
            private Mock<IService1> IService1Mock;
            private Mock<IPayRollView> IPayRollViewMock;
            private PayRollViewPresenter payrollviewpresenter;
            private List<DTOAuthor> authors;
            private DTOPayroll dtopay;
    
            [SetUp]
            public void TheSetup()
            {
                IService1Mock = new Mock<IService1>(); 
                IPayRollViewMock = new Mock<IPayRollView>();
    
                InitSetup();
                payrollviewpresenter = new PayRollViewPresenter(IPayRollViewMock.Object, IService1Mock.Object);
            }
    
            private void InitSetup()
            {
                 dtopay = new DTOPayroll {AuthorID = 1, PayRollID = 2, Salary = 3};
    
                IService1Mock.Setup(x => x.GetPayRollByAuthorID(It.IsAny<int>())).Returns(dtopay);
                IService1Mock.Setup(x => x.UpdatePayRollSalary(It.IsAny<DTOPayroll>()));
    
                 authors = new List<DTOAuthor>
                                  {
                                      new DTOAuthor {AuthorID = 1, FirstName = "help1", LastName = "test1"},
                                      new DTOAuthor {AuthorID = 2, FirstName = "help2", LastName = "test2"}
                                  };
    
                IService1Mock.Setup(x => x.GetAuthors()).Returns(authors);
                
                var drp = new DropDownList();
                drp.Items.Add("test");
                drp.Items.Add("test");
                drp.SelectedIndex = 0;
               
                IPayRollViewMock.SetupProperty(x => x.DtoAuthors, authors);
                IPayRollViewMock.SetupProperty(x => x.DrpAuthors, drp);
                IPayRollViewMock.SetupProperty(x => x.LblPayRollId, new Label());
    
                IPayRollViewMock.Object.IsPostback = false;
    
                var txtbox = new TextBox {Text = "1"};
    
                IPayRollViewMock.SetupProperty(x => x.TbxSalary, txtbox);
    
                var lbl = new Label {Text = "0"};
    
                IPayRollViewMock.SetupProperty(x => x.LblPayRollId, lbl);
    
                IService1Mock.Setup(x => x.AddPayRoll(It.IsAny<DTOPayroll>())).Returns(dtopay);
                IService1Mock.Setup(x => x.DeletePayRoll(It.IsAny<DTOPayroll>()));
            }
    
            [Test]
            public void PayRollViewPresenter_PageLoad()
            {
                payrollviewpresenter.PageLoad();
                IService1Mock.Verify((x => x.GetAuthors()), Times.AtLeastOnce());
                IService1Mock.Verify((x => x.GetPayRollByAuthorID(1)), Times.AtLeastOnce());
            }
    
            [Test]
            public void PayRollViewPresenter_drpAuthors_SelectedIndexChanged()
            {
                payrollviewpresenter.DRPAuthors_SelectedIndexChanged();
                IService1Mock.Verify((x => x.GetPayRollByAuthorID(1)), Times.AtLeastOnce());
            }
    
            [Test]
            public void PayRollViewPresenter_btnUpdateClick()
            {
                dtopay = new DTOPayroll { AuthorID = 1, PayRollID = 2, Salary = 3 };
    
                payrollviewpresenter.btnUpdateClick();
                IService1Mock.Verify((x => x.GetPayRollByAuthorID(1)), Times.AtLeastOnce());
                IService1Mock.Verify((x => x.UpdatePayRollSalary(dtopay)), Times.AtMostOnce());
            }
    
            [Test]
            public void PayRollViewPresenter_btnAddClick()
            {
                payrollviewpresenter.btnAddClick();
    
                Assert.AreEqual(IPayRollViewMock.Object.LblPayRollId.Text, "2");
                Assert.AreEqual(IPayRollViewMock.Object.TbxSalary.Text, "3");
    
                IService1Mock.Verify((x => x.AddPayRoll(dtopay)), Times.AtMostOnce());
            }
    
            [Test]
            public void PayRollViewPresenter_btnDeleteCick()
            {
                IPayRollViewMock.Object.LblPayRollId.Text = "2";
                payrollviewpresenter.btnDeleteClick();
    
                Assert.AreEqual(IPayRollViewMock.Object.LblPayRollId.Text, "2");
                Assert.AreEqual(IPayRollViewMock.Object.TbxSalary.Text, "3");
    
                IService1Mock.Verify((x => x.DeletePayRoll(dtopay)), Times.AtMostOnce());
                IService1Mock.Verify((x => x.GetPayRollByAuthorID(1)), Times.AtLeastOnce());
      
            }
    
        }
    }
    

      
    • Proposed as answer by lake Xiao Wednesday, January 6, 2016 8:34 AM
    Tuesday, December 29, 2015 5:48 AM