views:

797

answers:

2

Need some pointers for this. Found this and this, but I'm still kind a confused.

I just want to mock ActionExecutedContext, pass it, let filter to work a bit and check result.

Any help?

Source of filter you can find here
(it's changed a bit, but that's not a point at the moment).

So - i want unit test, that RememberUrl filter is smart enough to save current URL in session.

+2  A: 

1) Mocking Request.Url in ActionExecutedContext:

var request = new Mock<HttpRequestBase>();
request.SetupGet(r => r.HttpMethod).Returns("GET");
request.SetupGet(r => r.Url).Returns(new Url("http://somesite/action"));

var httpContext = new Mock<HttpContextBase>();
httpContext.SetupGet(c => c.Request).Returns(request.Object);

var actionExecutedContext = new Mock<ActionExecutedContext>();
actionExecutedContext.SetupGet(c => c.HttpContext).Returns(httpContext.Object);

2) I suppose you are injecting session wrapper in your RememberUrlAttribute's public constructor.

var rememberUrl = new RememberUrlAttribute(yourSessionWrapper);

rememberUrl.OnActionExecuted(actionExecutedContext.Object);

// and then check what is in your SessionWrapper
eu-ge-ne
rememberUrl haven't "ActionExecutedCotnext" method.
Arnis L.
It's OnActionExecuted. Anyway - it seems that i finally got it. Just need to properly mock httpContext. Thanks again - I really appreciate Your help. :)
Arnis L.
....... Fixed :)
eu-ge-ne
any ideas how to test something like this: http://gist.github.com/raw/88936/deadfa9378317706739fb802f3967efe6b148657/MVC%20LocalizationHelper.cs ? :)
Arnis L.
I would start from mocking `ViewContext` class
eu-ge-ne
+3  A: 

This is the result:

#region usages

using System;
using System.Collections.Specialized;
using System.Web;
using System.Web.Mvc;
using x.TestBase;
using x.UI.y.Infrastructure.Enums;
using x.UI.y.Infrastructure.Filters;
using x.UI.y.Test.Mocks;
using Moq;

//considering switch to NUnit... :D
using Microsoft.VisualStudio.TestTools.UnitTesting;

#endregion

namespace x.UI.y.Test.Unit.Infrastructure.Filters
{
    [TestClass]
    public class RememberUrlTester : TesterBase
    {
        private static HttpContextBaseMock _context = 
            new HttpContextBaseMock();
        private static ActionExecutedContextMock _actionContext = 
            new ActionExecutedContextMock(_context.Object);

        [TestMethod]
        //"Can save url in session" (i prefer test names in my own language :)
        public void SpeejPieglabaatUrlSesijaa()
        {
            //Arrange
            const string _url = "http://www.foo.bar/foo?bar=bar";
            _context.RequestMock.SetUrl(_url);    
            var filter = new RememberUrlAttribute();

            //Act
            filter.OnActionExecuted(_actionContext.Object);

            //Assert
            _context.SessionMock.Verify
                (m => m.Add(SessionKey.PreviousUrl.ToString(), _url));
        }
    }
}

Wrapped Mock<HttpWhatever> to keep tests clean.

I'm sure things can be done better, but I think it's a great start and I'm feeling quite excited.

Finally that HttpContext monster is under control! ^^

Arnis L.
Why NUnit? IMHO xUnit.NET is much better ;)
eu-ge-ne
Could give some arguments for that?
Arnis L.
ZeroBugBounce
@richdiet this one was long ago. I would prefer xUnit nowadays.
Arnis L.
Heh, sorry, me not paying attention to years!
ZeroBugBounce