Mocking IPrincipal User with MvcContrib.TestHelper

Share on Facebook

Recently I needed to mock an IPrincipal implementation while testing an Mvc web app I was writting. I eventually managed to piece together the scattered bits of information required to achieve this. However I felt the information was not presented in any coherent manner while searching so If you need to do similar, here you go. I believe the code is reasonably self documenting. I am using the mvccontrib testhelper with Rhino Mocks.

namespace Commerce.MvcWeb.Tests.Controllers

{

    [TestClass]

    public class BasketControllerTests : TestBase

    {

        private IBasketRepository mockBasketRepo;

        private BasketController ControllerUnderTest;

        [TestInitialize]

        public void ChildSetUp()

        {           

            mockBasketRepo = MockRepository.GenerateMock();

            mockBasketRepo.Stub(x => x.Config).Return(mockConfig);

        }

        [TestMethod]

        public void Test_Basket_Index_Returns_Model_To_Index_View()

        {

  //Arrange                              

            List items = new List();

            for (int i = 0; i < 5; i++) { items.Add(new BasketItem { ProductID = i, ProductName = "product" + i.ToString(), Quantity = i,  PricePaid = (Decimal)i, LineTotal = (Decimal)0.175 }); }

            Basket basket = new Basket { Items = items, UserName = "Administrator", OrderTotal = 200, SubTotalAmount = 150 }; 

            mockBasketRepo.Expect(x => x.GetBasket("Administrator")).Return(basket);

            //Act

            ControllerUnderTest = new BasketController(mockBasketRepo);

            builder.InitializeController(ControllerUnderTest);                        

            ViewResult result = ControllerUnderTest.Index()  as ViewResult;

            //Assert

            ControllerUnderTest.Index().AssertViewRendered();            

        }

    }

}

namespace Commerce.MvcWeb.Tests.Controllers

{

    [TestClass]

    public class TestBase

    {

        protected IConfiguration mockConfig;

        protected TestControllerBuilder builder;

        [TestInitialize]

        public virtual void SetUp()

        {

            builder = new TestControllerBuilder();

            //Initialise fake Configuration for testing

            mockConfig = MockRepository.GenerateMock();

            mockConfig.Stub(x => x.SiteTitle).Return("Welcome to Test Site");

            mockConfig.Stub(x => x.SiteDescription).Return("Description of Test Site");

            mockConfig.Stub(x => x.SiteKeywords).Return("Description,of,Test,Site");

            //Initialise fake HttpContext on Test Builder Object for Testing 

            System.Web.HttpCookieCollection cookies = new System.Web.HttpCookieCollection();

            cookies.Add(new System.Web.HttpCookie("ShopperID", "Administrator"));

            builder.HttpContext.Request.Expect(x => x.Cookies).Return(cookies);

            FakeIdentity fakeID = new FakeIdentity("Administrator");

            FakePrincipal fakeUser = new FakePrincipal(fakeID, null);

            builder.HttpContext.User = fakeUser;

            Uri Url = new Uri(@"http://localhost/xxx");

            builder.HttpContext.Request.Expect(x => x.Url).Return(Url);

            //builder.HttpContext.Request.Expect(x => x.ServerVariables).Return(new NameValueCollection());

        }

    }

}

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkListkick it on DotNetKicks.comTwitThis

Comments

Comments are closed

About Me

When not scratching my head for solutions to software challenges, I spend my time playing with my little boy - Michael Jnr.

Quotations

"All great things are simple, and many can be expressed in single words: freedom, justice, honor, duty, mercy, hope."
Sir Winston Churchill

Donate with PayPal - it

Calendar

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2005 - 2010

Search