Last active
December 11, 2017 21:07
-
-
Save janosorcsik/187ea66327f12c0843152ebc5063c50c to your computer and use it in GitHub Desktop.
Unit test extensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Data.Entity; | |
using System.IO; | |
using System.Linq; | |
using System.Reflection; | |
using System.Web; | |
using System.Web.Mvc; | |
using System.Web.Routing; | |
using Web.App_Start; | |
using Web.Components.Controllers; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
using NSubstitute; | |
namespace TestUtils | |
{ | |
public static class TestExtensions | |
{ | |
public static IDbSet<T> CreateMockDbSet<T>(this IQueryable<T> data) where T : class | |
{ | |
var mock = Substitute.For<IDbSet<T>, IQueryable<T>>(null); | |
mock.Provider.Returns(data.Provider, null); | |
mock.Expression.Returns(data.Expression, null); | |
mock.ElementType.Returns(data.ElementType, null); | |
mock.GetEnumerator().Returns(data.GetEnumerator(), null); | |
return mock; | |
} | |
public static Type[] GetTypesInNamespace(this Assembly assembly, string nameSpace) | |
{ | |
return | |
assembly.GetTypes() | |
.Where(t => String.Equals(t.Namespace, nameSpace, StringComparison.Ordinal)) | |
.ToArray(); | |
} | |
public static void SetControllerContext(this BaseController controller) | |
{ | |
HttpContextBase httpContextMock = Substitute.For<HttpContextBase>(null); | |
HttpRequestBase httpRequestMock = Substitute.For<HttpRequestBase>(null); | |
HttpResponseBase httpResponseMock = Substitute.For<HttpResponseBase>(null); | |
httpResponseMock.ApplyAppPathModifier(Arg.Any<string>()).Returns(ctx => ctx.Arg<string>(), null); | |
httpContextMock.Request.Returns(httpRequestMock, null); | |
httpContextMock.Response.Returns(httpResponseMock, null); | |
RequestContext requestContext = new RequestContext(httpContextMock, new RouteData()); | |
ControllerContext controllerContext = new ControllerContext(requestContext, controller); | |
controller.ControllerContext = controllerContext; | |
} | |
public static void SetControllerUrl(this BaseController baseController) | |
{ | |
RouteCollection routes = new RouteCollection(); | |
RouteConfig.RegisterRoutes(routes); | |
baseController.Url = new UrlHelper(baseController.ControllerContext.RequestContext, routes); | |
} | |
public static T Throws<T>(Action action) where T : Exception | |
{ | |
try | |
{ | |
action(); | |
} | |
catch (T ex) | |
{ | |
return ex; | |
} | |
catch (Exception ex) | |
{ | |
Type actualExcpetionType = ex.GetBaseException().GetType(); | |
Type expectedExcpetionType = typeof (T); | |
Assert.Fail( | |
String.Format("Kivétel típusa: {0}, visszakapott: {1}", actualExcpetionType, expectedExcpetionType), | |
null); | |
} | |
return null; | |
} | |
public static HttpContext GetMockHttpContext() | |
{ | |
return new HttpContext(new HttpRequest(null, "http://tempuri.org", null), new HttpResponse(null)); | |
} | |
public static HttpPostedFileBase GetMockHttpPostedFileBase(int? contentLength = null, string fileName = null) | |
{ | |
HttpPostedFileBase file = Substitute.For<HttpPostedFileBase>(null); | |
if (String.IsNullOrEmpty(fileName)) | |
{ | |
file.FileName.Returns("tests.xls", null); | |
} | |
else | |
{ | |
FileStream stream = new FileStream(fileName, FileMode.Open); | |
file.InputStream.Returns(stream, null); | |
file.ContentLength.Returns((int) stream.Length, null); | |
file.FileName.Returns(stream.Name, null); | |
} | |
if (contentLength.HasValue) | |
{ | |
file.ContentLength.Returns(contentLength.Value, null); | |
} | |
return file; | |
} | |
public static bool InheritsFrom(this Type t, Type baseType) | |
{ | |
Type current = t.BaseType; | |
while (current != null) | |
{ | |
if (current == baseType) | |
{ | |
return true; | |
} | |
current = current.BaseType; | |
} | |
return false; | |
} | |
public static string[] GetLinesFromDefinition(string definition) | |
{ | |
return definition.Split(new[] {","}, StringSplitOptions.RemoveEmptyEntries); | |
} | |
public static List<string> AddColumnToColumns(List<string> columns, string column) | |
{ | |
if (!String.IsNullOrEmpty(column)) | |
{ | |
columns.Add(column); | |
} | |
return columns; | |
} | |
} | |
public class DbInfo | |
{ | |
public string Name { get; set; } | |
public string Sql { get; set; } | |
} | |
public class AsItemValue | |
{ | |
public long AsValueId { get; set; } | |
public string Value { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment