MockExtensions.cs :  » Bloggers » SubText » UnitTests » Subtext » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Bloggers » SubText 
SubText » UnitTests » Subtext » MockExtensions.cs
using System;
using System.Collections;
using System.Collections.Specialized;
using System.IO;
using System.Web;
using System.Web.Caching;
using System.Web.Routing;
using Moq;
using Moq.Stub;
using Subtext.Extensibility;
using Subtext.Framework;
using Subtext.Framework.Components;
using Subtext.Framework.Providers;
using Subtext.Framework.Routing;
using Subtext.Framework.Web.HttpModules;

namespace UnitTests.Subtext{
    public static class MockExtensions
    {
        public static Mock<HttpContextBase> FakeRequest(this Mock<HttpContextBase> httpContextMock, string virtualPath)
        {
            httpContextMock.FakeRequest(virtualPath, null);
            return httpContextMock;
        }

        public static StringWriter FakeSitemapHandlerRequest(this Mock<ISubtextContext> subtextContext,
                                                             Mock<ObjectProvider> repository)
        {
            subtextContext.Setup(c => c.Repository).Returns(repository.Object);
            StringWriter writer = subtextContext.FakeSubtextContextRequest(new Blog {Host = "localhost"}, "/sitemap.xml",
                                                                           "/", string.Empty);
            Mock<UrlHelper> urlHelper = Mock.Get(subtextContext.Object.UrlHelper);
            urlHelper.Setup(u => u.EntryUrl(It.IsAny<Entry>())).Returns<Entry>(
                e =>
                e.PostType == PostType.BlogPost
                    ? "/some-blogpost-with-id-of-" + e.Id
                    : "some-article-with-id-of-" + e.Id);
            urlHelper.Setup(u => u.BlogUrl()).Returns("/");
            urlHelper.Setup(u => u.ContactFormUrl()).Returns("/contact.aspx");
            return writer;
        }

        public static StringWriter FakeRequest(this Mock<HttpContextBase> httpContextMock, string virtualPath,
                                               string subfolder)
        {
            return httpContextMock.FakeRequest(virtualPath, subfolder, "~/");
        }

        public static StringWriter FakeRequest(this Mock<HttpContextBase> httpContextMock, string virtualPath,
                                               string subfolder, string applicationPath)
        {
            httpContextMock.Setup(h => h.Request.HttpMethod).Returns("GET");
            httpContextMock.Setup(context => context.Request.ApplicationPath).Returns(applicationPath);
            httpContextMock.Setup(context => context.Request.AppRelativeCurrentExecutionFilePath).Returns(virtualPath);
            httpContextMock.Setup(context => context.Request.Path).Returns(virtualPath);
            httpContextMock.Setup(context => context.Request.FilePath).Returns(virtualPath);
            httpContextMock.SetupGet(c => c.Items[BlogRequest.BlogRequestKey]).Returns(new BlogRequest("localhost",
                                                                                                       subfolder,
                                                                                                       new Uri(
                                                                                                           "http://localhost/"),
                                                                                                       true));
            var writer = new StringWriter();
            httpContextMock.Setup(c => c.Response.Output).Returns(writer);
            return writer;
        }

        public static void FakeSyndicationContext(this Mock<ISubtextContext> subtextContextMock, Blog blog,
                                                  string virtualPath, string applicationPath, Action<string> callback)
        {
            subtextContextMock.FakeSyndicationContext(blog, virtualPath, applicationPath, null, callback);
        }

        public static StringWriter FakeSubtextContextRequest(this Mock<ISubtextContext> subtextContextMock, Blog blog,
                                                             string virtualPath, string applicationPath,
                                                             string subfolder)
        {
            var httpContext = new Mock<HttpContextBase>();
            StringWriter writer = httpContext.FakeRequest(virtualPath, subfolder);
            httpContext.SetupApplicationPath(applicationPath);
            httpContext.Stub(h => h.Response.ContentType);
            var hashTable = new Hashtable();
            httpContext.Setup(c => c.Items).Returns(hashTable);
            var urlHelper = new Mock<UrlHelper>();

            var routeData = new RouteData();
            routeData.Values.Add("subfolder", subfolder);

            subtextContextMock.Setup(c => c.HttpContext).Returns(httpContext.Object);

            subtextContextMock.SetupBlog(blog)
                .SetupUrlHelper(urlHelper)
                .SetupRequestContext(httpContext, routeData);

            return writer;
        }

        public static Mock<HttpContextBase> SetupApplicationPath(this Mock<HttpContextBase> httpContext,
                                                                 string applicationPath)
        {
            httpContext.Setup(c => c.Request.ApplicationPath).Returns(applicationPath);
            return httpContext;
        }

        public static Mock<ISubtextContext> SetupRequestContext(this Mock<ISubtextContext> context,
                                                                Mock<HttpContextBase> httpContext, RouteData routeData)
        {
            return context.SetupRequestContext(httpContext, routeData, null);
        }

        public static Mock<ISubtextContext> SetupBlog(this Mock<ISubtextContext> context, Blog blog)
        {
            context.Setup(c => c.Blog).Returns(blog);
            return context;
        }

        public static Mock<ISubtextContext> SetupUrlHelper(this Mock<ISubtextContext> context, Mock<UrlHelper> urlHelper)
        {
            context.Setup(c => c.UrlHelper).Returns(urlHelper.Object);
            return context;
        }

        public static Mock<ISubtextContext> SetupUrlHelper(this Mock<ISubtextContext> context, UrlHelper urlHelper)
        {
            context.Setup(c => c.UrlHelper).Returns(urlHelper);
            return context;
        }

        public static Mock<ISubtextContext> SetupRepository(this Mock<ISubtextContext> context,
                                                            Mock<ObjectProvider> repository)
        {
            context.Setup(c => c.Repository).Returns(repository.Object);
            return context;
        }

        public static Mock<ISubtextContext> SetupRepository(this Mock<ISubtextContext> context,
                                                            ObjectProvider repository)
        {
            context.Setup(c => c.Repository).Returns(repository);
            return context;
        }

        public static Mock<ISubtextContext> SetupRequestContext(this Mock<ISubtextContext> context,
                                                                Mock<HttpContextBase> httpContext, RouteData routeData,
                                                                Blog blog)
        {
            httpContext = httpContext ?? new Mock<HttpContextBase>();
            routeData = routeData ?? new RouteData();
            httpContext.Setup(c => c.Items).Returns(new Hashtable());
            var requestContext = new RequestContext(httpContext.Object, routeData);
            context.SetupRequestContext(requestContext);
            context.Setup(c => c.Cache).Returns(new TestCache());
            context.Setup(c => c.Blog).Returns(blog ?? new Blog());
            context.Setup(c => c.HttpContext.Items).Returns(new Hashtable());
            return context;
        }

        public static Mock<ISubtextContext> SetupRequestContext(this Mock<ISubtextContext> context,
                                                                RequestContext requestContext)
        {
            context.Setup(c => c.RequestContext).Returns(requestContext);
            return context;
        }

        public static void FakeSyndicationContext(this Mock<ISubtextContext> subtextContextMock, Blog blog,
                                                  string virtualPath, string applicationPath, string subfolder,
                                                  Action<string> callback)
        {
            var urlHelper = new Mock<UrlHelper>();
            var httpContext = new Mock<HttpContextBase>();
            httpContext.FakeSyndicationRequest(virtualPath, applicationPath, callback);
            var hashTable = new Hashtable();
            httpContext.Setup(c => c.Items).Returns(hashTable);
            subtextContextMock.Setup(c => c.Cache).Returns(new TestCache());
            subtextContextMock.Setup(c => c.HttpContext).Returns(httpContext.Object);

            string imagePath = "/images/RSS2Image.gif";
            if(applicationPath != "/")
            {
                imagePath = applicationPath + imagePath;
            }

            urlHelper.Setup(url => url.ImageUrl(It.IsAny<string>())).Returns(new VirtualPath(imagePath));
            urlHelper.Setup(u => u.AggBugUrl(It.IsAny<int>())).Returns<int>(id => "/Subtext.Web/aggbug/" + id + ".aspx");
            urlHelper.Setup(u => u.CommentRssUrl(It.IsAny<int>())).Returns<int>(
                id => "/Subtext.Web/comments/commentRss/" + id + ".aspx");
            urlHelper.Setup(u => u.TrackbacksUrl(It.IsAny<int>())).Returns<int>(
                id => "/Subtext.Web/services/trackbacks/" + id + ".aspx");

            var routeData = new RouteData();
            routeData.Values.Add("subfolder", subfolder);
            var requestContext = new RequestContext(httpContext.Object, routeData);
            subtextContextMock.SetupBlog(blog);
            subtextContextMock.SetupUrlHelper(urlHelper.Object);
            subtextContextMock.SetupRequestContext(requestContext);
            subtextContextMock.Setup(c => c.Cache).Returns(new TestCache());
        }

        public static void FakeSyndicationContext(this Mock<ISubtextContext> subtextContextMock, Blog blog,
                                                  string virtualPath, Action<string> callback)
        {
            subtextContextMock.FakeSyndicationContext(blog, virtualPath, "/", callback);
        }

        public static void FakeSyndicationRequest(this Mock<HttpContextBase> httpContextMock, string virtualPath,
                                                  string applicationPath, Action<string> callback)
        {
            var headers = new NameValueCollection {{"If-Modified-Since", null}};
            httpContextMock.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns(virtualPath);
            httpContextMock.Setup(c => c.Request.Path).Returns(virtualPath);
            httpContextMock.Setup(c => c.Request.FilePath).Returns(virtualPath);
            httpContextMock.Setup(c => c.Request.ApplicationPath).Returns(applicationPath);
            httpContextMock.Setup(c => c.Response.Output).Returns(new StringWriter());
            httpContextMock.Setup(c => c.Request.Headers).Returns(headers);
            httpContextMock.SetupSet(c => c.Response.ContentType, "text/xml");
            httpContextMock.Setup(c => c.Response.Cache.SetCacheability(HttpCacheability.Public));
            httpContextMock.Setup(c => c.Response.Cache.SetLastModified(It.IsAny<DateTime>()));
            httpContextMock.Setup(c => c.Response.Cache.SetETag(It.IsAny<string>()));
            httpContextMock.Setup(c => c.Response.AddHeader(It.IsAny<string>(), It.IsAny<string>()));
            httpContextMock.SetupSet(c => c.Response.StatusCode, It.IsAny<int>());
            if(callback != null)
            {
                httpContextMock.Setup(c => c.Response.Write(It.IsAny<string>())).Callback(callback);
            }
            httpContextMock.Setup(c => c.Cache).Returns((Cache)null);
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.