本文整理汇总了C#中Mock.Stub方法的典型用法代码示例。如果您正苦于以下问题:C# Mock.Stub方法的具体用法?C# Mock.Stub怎么用?C# Mock.Stub使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Mock
的用法示例。
在下文中一共展示了Mock.Stub方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: OpmlHandler_WithRequestForAggregateBlog_GetsGroupIdFromQueryString
public void OpmlHandler_WithRequestForAggregateBlog_GetsGroupIdFromQueryString()
{
//arrange
var queryString = new NameValueCollection { { "GroupID", "310" } };
var context = new Mock<ISubtextContext>();
context.Stub(c => c.HttpContext.Response.ContentType);
context.Setup(c => c.HttpContext.Response.Output).Returns(new StringWriter());
context.Setup(c => c.HttpContext.Request.QueryString).Returns(queryString);
context.Setup(c => c.HttpContext.Request.Url).Returns(new Uri("http://example.com/"));
context.SetupUrlHelper(new Mock<BlogUrlHelper>());
var repository = new Mock<ObjectRepository>();
int? parsedGroupId = null;
repository.Setup(r => r.GetBlogsByGroup("example.com", It.IsAny<int?>())).Callback<string, int?>(
(host, groupId) => parsedGroupId = groupId);
context.SetupRepository(repository);
var writer = new Mock<OpmlWriter>();
writer.Setup(w => w.Write(It.IsAny<IEnumerable<Blog>>(), It.IsAny<TextWriter>(), It.IsAny<BlogUrlHelper>()));
var appSettings = new NameValueCollection();
appSettings.Add("AggregateEnabled", "true");
var hostInfo = new HostInfo(appSettings);
var handler = new OpmlHandler(context.Object, writer.Object, new LazyNotNull<HostInfo>(() => hostInfo));
//act
handler.ProcessRequest();
//assert
Assert.AreEqual(310, parsedGroupId.Value);
}
示例2: AuthenticateRequest_WithRequestHavingValidAuthCookies_SetsUserToGenericPrincipalWithRoles
public void AuthenticateRequest_WithRequestHavingValidAuthCookies_SetsUserToGenericPrincipalWithRoles()
{
// arrange
var module = new AuthenticationModule();
const string roles = "Admins|HostAdmins|Users";
var ticket = new FormsAuthenticationTicket(1, ".ASPXAUTH.42", DateTime.Now, DateTime.Now.AddDays(60), true,
roles);
string cookieValue = FormsAuthentication.Encrypt(ticket);
var authCookie = new HttpCookie(".ASPXAUTH.42") { Value = cookieValue };
var cookies = new HttpCookieCollection { authCookie };
var httpContext = new Mock<HttpContextBase>();
httpContext.Stub(c => c.User);
httpContext.Setup(c => c.Request.Path).Returns("/");
httpContext.Setup(c => c.Request.QueryString).Returns(new NameValueCollection());
httpContext.Setup(c => c.Request.Cookies).Returns(cookies);
httpContext.Setup(c => c.Response.Cookies).Returns(cookies);
var blogRequest = new BlogRequest("localhost", string.Empty, new Uri("http://localhost"), false,
RequestLocation.Blog, "/") { Blog = new Blog { Id = 42 } };
// act
module.AuthenticateRequest(httpContext.Object, blogRequest);
// assert
var principal = httpContext.Object.User as GenericPrincipal;
Assert.IsNotNull(principal);
Assert.IsTrue(principal.IsInRole("Admins"));
Assert.IsTrue(principal.IsInRole("HostAdmins"));
Assert.IsTrue(principal.IsInRole("Users"));
}
示例3: NotModifiedResultSends304StatusCodeAndSuppressesContent
public void NotModifiedResultSends304StatusCodeAndSuppressesContent()
{
// arrange
var result = new NotModifiedResult();
var httpContext = new Mock<HttpContextBase>();
httpContext.Stub(h => h.Response.StatusCode);
httpContext.Stub(h => h.Response.SuppressContent);
var controllerContext = new ControllerContext();
controllerContext.HttpContext = httpContext.Object;
// act
result.ExecuteResult(controllerContext);
// assert
Assert.AreEqual(304, httpContext.Object.Response.StatusCode);
Assert.IsTrue(httpContext.Object.Response.SuppressContent);
}
示例4: RenderViewAndRestoreContentType
public void RenderViewAndRestoreContentType() {
// Arrange
Mock<ViewContext> mockViewContext = new Mock<ViewContext>();
mockViewContext.Stub(c => c.HttpContext.Response.ContentType);
ViewContext vc = mockViewContext.Object;
Mock<ViewPage> mockViewPage = new Mock<ViewPage>();
mockViewPage.Expect(vp => vp.RenderView(vc)).Callback(() => vc.HttpContext.Response.ContentType = "newContentType");
// Act
vc.HttpContext.Response.ContentType = "oldContentType";
ViewUserControl.RenderViewAndRestoreContentType(mockViewPage.Object, vc);
string postContentType = vc.HttpContext.Response.ContentType;
// Assert
Assert.AreEqual("oldContentType", postContentType);
}
示例5: OpmlHandler_WithRequest_SetsContentTypeToXml
public void OpmlHandler_WithRequest_SetsContentTypeToXml()
{
//arrange
var context = new Mock<ISubtextContext>();
context.Stub(c => c.HttpContext.Response.ContentType);
context.Setup(c => c.HttpContext.Response.Output).Returns(new StringWriter());
context.SetupUrlHelper(new Mock<UrlHelper>());
var writer = new Mock<OpmlWriter>();
writer.Setup(w => w.Write(It.IsAny<IEnumerable<Blog>>(), It.IsAny<TextWriter>(), It.IsAny<UrlHelper>()));
var handler = new OpmlHandler(context.Object, writer.Object);
//act
handler.ProcessRequest(new HostInfo());
//assert
Assert.AreEqual("text/xml", context.Object.HttpContext.Response.ContentType);
}
示例6: FakeSubtextContextRequest
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<BlogUrlHelper>();
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;
}
示例7: OpmlHandler_WithRequest_SetsContentTypeToXml
public void OpmlHandler_WithRequest_SetsContentTypeToXml()
{
//arrange
var repository = new Mock<ObjectRepository>();
repository.Setup(r => r.GetBlogsByGroup("http://subtextproject.com/", 1)).Returns(new Blog[] { new Blog { } });
var queryString = new NameValueCollection();
queryString.Add("GroupID", "1");
var context = new Mock<ISubtextContext>();
context.Setup(c => c.Repository).Returns(repository.Object);
context.Setup(c => c.HttpContext.Request.Url).Returns(new Uri("http://subtextproject.com/"));
context.Setup(c => c.HttpContext.Request.QueryString).Returns(queryString);
context.Stub(c => c.HttpContext.Response.ContentType);
context.Setup(c => c.HttpContext.Response.Output).Returns(new StringWriter());
context.SetupUrlHelper(new Mock<BlogUrlHelper>());
var writer = new Mock<OpmlWriter>();
writer.Setup(w => w.Write(It.IsAny<IEnumerable<Blog>>(), It.IsAny<TextWriter>(), It.IsAny<BlogUrlHelper>()));
var handler = new OpmlHandler(context.Object, writer.Object, new LazyNotNull<HostInfo>(CreateHostInfo));
//act
handler.ProcessRequest();
//assert
Assert.AreEqual("text/xml", context.Object.HttpContext.Response.ContentType);
}
示例8: ConvertRequestToBlogRequest_WithRequestForAlternateHost_RedirectsToPrimaryHost
public void ConvertRequestToBlogRequest_WithRequestForAlternateHost_RedirectsToPrimaryHost()
{
//arrange
var service = new Mock<IBlogLookupService>();
service.Setup(s => s.Lookup(It.IsAny<BlogRequest>())).Returns(new BlogLookupResult(null,
new Uri(
"http://www.example.com/")));
var httpResponse = new Mock<HttpResponseBase>();
httpResponse.Stub(r => r.StatusCode);
httpResponse.Stub(r => r.StatusDescription);
httpResponse.Stub(r => r.RedirectLocation);
Mock<HttpRequestBase> httpRequest = CreateRequest("example.com", "/", "/", true);
var httpContext = new Mock<HttpContextBase>();
httpContext.Setup(c => c.Request).Returns(httpRequest.Object);
httpContext.Setup(c => c.Response).Returns(httpResponse.Object);
var module = new BlogRequestModule(service.Object);
//act
BlogRequest request = module.ConvertRequestToBlogRequest(httpContext.Object);
//assert
Assert.IsNull(request);
httpResponse.Verify(r => r.End());
Assert.AreEqual(301, httpResponse.Object.StatusCode);
Assert.AreEqual("301 Moved Permanently", httpResponse.Object.StatusDescription);
Assert.AreEqual("http://www.example.com/", httpResponse.Object.RedirectLocation);
}
示例9: HandleFormsAuthenticationTicket_WithRequestHavingNullAuthTicket_WritesExpiredCookie
public void HandleFormsAuthenticationTicket_WithRequestHavingNullAuthTicket_WritesExpiredCookie()
{
// arrange
Func<BlogRequest, HttpContextBase, string> loginFunc = (r, c) => "/foo/login.aspx";
var module = new AuthenticationModule();
var authCookie = new HttpCookie(".ASPXAUTH.42") { Value = null };
var cookies = new HttpCookieCollection { authCookie };
var httpContext = new Mock<HttpContextBase>();
httpContext.Stub(c => c.User);
httpContext.Setup(c => c.Request.Path).Returns("/");
httpContext.Setup(c => c.Request.QueryString).Returns(new NameValueCollection());
httpContext.Setup(c => c.Request.Cookies).Returns(cookies);
httpContext.Setup(c => c.Response.Redirect(It.IsAny<string>(), true));
var responseCookies = new HttpCookieCollection();
httpContext.Setup(c => c.Response.Cookies).Returns(responseCookies);
var blogRequest = new BlogRequest("localhost", string.Empty, new Uri("http://localhost"), false,
RequestLocation.Blog, "/") { Blog = new Blog { Id = 42 } };
// act
module.HandleFormsAuthenticationTicket(blogRequest, httpContext.Object, null);
// assert
var principal = httpContext.Object.User as GenericPrincipal;
Assert.IsNull(principal);
Assert.AreEqual(1, responseCookies.Count);
HttpCookie cookie = responseCookies[".ASPXAUTH.42"];
Assert.IsTrue(cookie.Expires.AddYears(20) < DateTime.Now);
}
示例10: LoadControl_WithControlThrowingHttpParseException_LoadsErrorControlWithExceptionProperty
public void LoadControl_WithControlThrowingHttpParseException_LoadsErrorControlWithExceptionProperty()
{
// arrange
var containerControl = new Mock<IContainerControl>();
var exception = new HttpParseException();
var userControl = new Mock<UserControl>();
userControl.Stub(c => c.ID);
var errorControl = userControl.As<IErrorControl>();
userControl.Object.ID = "Foo.Bar";
errorControl.Stub(c => c.Exception);
containerControl.Setup(tc => tc.LoadControl("~/Skins/VsShirts/Controls/ViewPost.ascx")).Throws(exception);
containerControl.Setup(tc => tc.LoadControl("~/Skins/_System/Controls/Error.ascx")).Returns((UserControl)errorControl.Object);
var skin = new SkinConfig { TemplateFolder = "VsShirts" };
var skinControlLoader = new SkinControlLoader(containerControl.Object, skin);
// act
var control = skinControlLoader.LoadControl("ViewPost") as IErrorControl;
// assert
Assert.AreEqual(exception, control.Exception.InnerException);
}