Click here to Skip to main content
15,884,298 members
Articles / Desktop Programming / Windows Forms

Visual Application Launcher

Rate me:
Please Sign up or sign in to vote.
4.91/5 (37 votes)
23 Jan 2012CPOL23 min read 107K   3.7K   116  
A WinForms UI using WCF services, Entity Framework, repository data access, repository caching, Unit of Work, Dependency Injection, and every other buzz work you can think of!
namespace VAL.Tests
{
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using System;
    using System.Collections.Generic;
    using System.Linq;

    using VAL.Model;
    using VAL.Model.Entities;
    using VAL.BusinessService;
    using VAL.Core;
    using VAL.Contracts;
    using VAL.Data;

    using Moq;

    [TestClass]
    public class GroupTypeServiceTests
    {
        private readonly Mock<IRepository<GroupType>> _groupTypeRepository = new Mock<IRepository<GroupType>>();
        private readonly Mock<IDbContext> _dataContext = new Mock<IDbContext>();
        private IGroupTypeService Service { get; set; }

        [TestInitialize]
        public void TestInitialize()
        {
            var typeList = TestDataObjectCreator.GetGroupTypes();

            // Setup the mock repository to use a list of objects as it's query source
            _groupTypeRepository.Setup(g => g.Table).Returns(typeList.AsQueryable());

            // Setup the delete method to remove the item from the list we created above
            _groupTypeRepository.Setup(g => g.Delete(It.IsAny<GroupType>())).Callback
                ((GroupType item) => typeList.Remove(item));

            // Setup the delete method to remove the item from the list we created above
            _groupTypeRepository.Setup(g => g.Add(It.IsAny<GroupType>())).Callback
                ((GroupType item) =>
                {
                    if (item.Id == 0)
                    {
                        typeList.Add(item);
                    }
                });

            this.Service = new GroupTypeDomainService(_groupTypeRepository.Object,
                _dataContext.Object);
        }        

        [TestMethod]
        public void Get_Items_Returns_List_Data()
        {
            var items = Service.GetGroupTypes();

            Assert.IsNotNull(items);
            Assert.IsTrue(items.Length > 0);
        }
        [TestMethod]
        public void Add_Items_Creates_Repository_Data()
        {
            var oldCount = Service.GetGroupTypes().Length;

            var item = new GroupType
            {
                Description = "New Item"
            };
            Service.SaveGroupType(item);

            var newCount = Service.GetGroupTypes().Length;
            Assert.IsTrue(newCount > oldCount);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void Add_Item_With_Invalid_Object_Throws_Exception()
        {
            GroupType item = null;
            Service.SaveGroupType(item);
        }

        [TestMethod]
        [ExpectedException(typeof(BusinessRuleException))]
        public void Invalid_Object_Throws_Business_Rules_Exception()
        {
            GroupType item = new GroupType();
            Service.SaveGroupType(item);
        }

        [TestMethod]
        public void Delete_Item_Removes_Repository_Data()
        {
            var oldCount = Service.GetGroupTypes().Length;

            Service.DeleteGroupType(1);

            var newCount = Service.GetGroupTypes().Length;
            Assert.IsTrue(newCount < oldCount);
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidOperationException))]
        public void Delete_Item_With_Groups_Throws_Exception()
        {
            var item = (from gt in _groupTypeRepository.Object.Table
                       where gt.Id == 1
                       select gt).FirstOrDefault();

            item.Groups.Add(new Group { Id = 1, Description = "Test" });

            Service.DeleteGroupType(1);
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions