Skip to content

Instantly share code, notes, and snippets.

@MichaelaIvanova
Created September 1, 2017 13:35
Show Gist options
  • Save MichaelaIvanova/58da15c6093ac87088f7cdedcbfbdda9 to your computer and use it in GitHub Desktop.
Save MichaelaIvanova/58da15c6093ac87088f7cdedcbfbdda9 to your computer and use it in GitHub Desktop.
Umbraco Contex Mocker with mocket request and query string
public class ContextMocker
{
public ContextMocker(string [] qKeys = null)
{
ILogger loggerMock = Mock.Of<ILogger>();
IProfiler profilerMock = Mock.Of<IProfiler>();
var contextBaseMock = new Mock<HttpContextBase>();
if (qKeys != null)
{
var requestMock = new Mock<HttpRequestBase>();
requestMock.SetupGet(r => r.Url).Returns(new System.Uri("http://imaginary.com"));
var mockedQstring = new Mock<NameValueCollection>();
foreach(var k in qKeys)
{
mockedQstring.Setup(r => r.Get(It.Is<string>(s=>s.Contains(k)==true))).Returns("example_value_"+k);
}
requestMock.SetupGet(r => r.QueryString).Returns(mockedQstring.Object);
contextBaseMock.SetupGet(p => p.Request).Returns(requestMock.Object);
}
WebSecurity webSecurityMock = new Mock<WebSecurity>(null, null).Object;
IUmbracoSettingsSection umbracoSettingsSectionMock = Mock.Of<IUmbracoSettingsSection>();
this.ApplicationContextMock = new ApplicationContext(CacheHelper.CreateDisabledCacheHelper(), new ProfilingLogger(loggerMock, profilerMock));
this.UmbracoContextMock = UmbracoContext.EnsureContext(contextBaseMock.Object, this.ApplicationContextMock, webSecurityMock, umbracoSettingsSectionMock, Enumerable.Empty<IUrlProvider>(), true);
}
public ApplicationContext ApplicationContextMock { get; private set; }
public UmbracoContext UmbracoContextMock { get; private set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment