Skip to content

Commit

Permalink
update unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
liuliang-wt committed Jun 27, 2021
1 parent 47ce30d commit 3247a66
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -209,24 +209,26 @@ public string GetFirstError()

public static class MockWtmContext
{
public static WTMContext CreateWtmContext(IDataContext dataContext= null, string usercode = null)
public static WTMContext CreateWtmContext(IDataContext dataContext = null, string usercode = null)
{
GlobalData gd = new GlobalData();
gd.AllAccessUrls = new List<string>();
gd.AllAssembly = new List<System.Reflection.Assembly>();
gd.AllModule = new List<WalkingTec.Mvvm.Core.Support.Json.SimpleModule>();
gd.AllModule = new List<Core.Support.Json.SimpleModule>();

Mock<HttpContext> mockHttpContext = new Mock<HttpContext>();
Mock<HttpRequest> mockHttpRequest = new Mock<HttpRequest>();
Mock<IServiceProvider> mockService = new Mock<IServiceProvider>();
MockHttpSession mockSession = new MockHttpSession();
mockHttpRequest.Setup(x => x.Cookies).Returns(new MockCookie());
mockService.Setup(x => x.GetService(typeof(IDistributedCache))).Returns(new MemoryDistributedCache(Options.Create<MemoryDistributedCacheOptions>(new MemoryDistributedCacheOptions())));
var cache = new MemoryDistributedCache(Options.Create<MemoryDistributedCacheOptions>(new MemoryDistributedCacheOptions()));
var res = new ResourceManagerStringLocalizerFactory(Options.Create<LocalizationOptions>(new LocalizationOptions { ResourcesPath = "Resources" }), new Microsoft.Extensions.Logging.LoggerFactory());
mockService.Setup(x => x.GetService(typeof(IDistributedCache))).Returns(cache);
mockHttpContext.Setup(x => x.Request).Returns(mockHttpRequest.Object);
mockHttpContext.Setup(x => x.RequestServices).Returns(mockService.Object);
var httpa = new HttpContextAccessor();
httpa.HttpContext = mockHttpContext.Object;
var wtmcontext = new WTMContext(null, new GlobalData(), httpa, new DefaultUIService(), null,dataContext, new ResourceManagerStringLocalizerFactory(Options.Create<LocalizationOptions>(new LocalizationOptions { ResourcesPath = "Resources" }), new Microsoft.Extensions.Logging.LoggerFactory()));
var wtmcontext = new WTMContext(null, new GlobalData(), httpa, new DefaultUIService(), null, dataContext, res, cache: cache);
wtmcontext.MSD = new BasicMSD();
wtmcontext.Session = new SessionServiceProvider(mockSession);
if (dataContext == null)
Expand All @@ -240,7 +242,7 @@ public static WTMContext CreateWtmContext(IDataContext dataContext= null, string
wtmcontext.LoginUserInfo = new LoginUserInfo { ITCode = usercode ?? "user" };
wtmcontext.GlobaInfo.AllAccessUrls = new List<string>();
wtmcontext.GlobaInfo.AllAssembly = new List<System.Reflection.Assembly>();
wtmcontext.GlobaInfo.AllModule = new List<WalkingTec.Mvvm.Core.Support.Json.SimpleModule>();
wtmcontext.GlobaInfo.AllModule = new List<Core.Support.Json.SimpleModule>();
mockService.Setup(x => x.GetService(typeof(WtmFileProvider))).Returns(new WtmFileProvider(wtmcontext));
return wtmcontext;
}
Expand Down
12 changes: 7 additions & 5 deletions demo/WalkingTec.Mvvm.Demo.Test/MockUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,24 +209,26 @@ public string GetFirstError()

public static class MockWtmContext
{
public static WTMContext CreateWtmContext(IDataContext dataContext= null, string usercode = null)
public static WTMContext CreateWtmContext(IDataContext dataContext = null, string usercode = null)
{
GlobalData gd = new GlobalData();
gd.AllAccessUrls = new List<string>();
gd.AllAssembly = new List<System.Reflection.Assembly>();
gd.AllModule = new List<WalkingTec.Mvvm.Core.Support.Json.SimpleModule>();
gd.AllModule = new List<Core.Support.Json.SimpleModule>();

Mock<HttpContext> mockHttpContext = new Mock<HttpContext>();
Mock<HttpRequest> mockHttpRequest = new Mock<HttpRequest>();
Mock<IServiceProvider> mockService = new Mock<IServiceProvider>();
MockHttpSession mockSession = new MockHttpSession();
mockHttpRequest.Setup(x => x.Cookies).Returns(new MockCookie());
mockService.Setup(x => x.GetService(typeof(IDistributedCache))).Returns(new MemoryDistributedCache(Options.Create<MemoryDistributedCacheOptions>(new MemoryDistributedCacheOptions())));
var cache = new MemoryDistributedCache(Options.Create<MemoryDistributedCacheOptions>(new MemoryDistributedCacheOptions()));
var res = new ResourceManagerStringLocalizerFactory(Options.Create<LocalizationOptions>(new LocalizationOptions { ResourcesPath = "Resources" }), new Microsoft.Extensions.Logging.LoggerFactory());
mockService.Setup(x => x.GetService(typeof(IDistributedCache))).Returns(cache);
mockHttpContext.Setup(x => x.Request).Returns(mockHttpRequest.Object);
mockHttpContext.Setup(x => x.RequestServices).Returns(mockService.Object);
var httpa = new HttpContextAccessor();
httpa.HttpContext = mockHttpContext.Object;
var wtmcontext = new WTMContext(null, new GlobalData(), httpa, new DefaultUIService(), null,dataContext, new ResourceManagerStringLocalizerFactory(Options.Create<LocalizationOptions>(new LocalizationOptions { ResourcesPath = "Resources" }), new Microsoft.Extensions.Logging.LoggerFactory()));
var wtmcontext = new WTMContext(null, new GlobalData(), httpa, new DefaultUIService(), null, dataContext, res, cache: cache);
wtmcontext.MSD = new BasicMSD();
wtmcontext.Session = new SessionServiceProvider(mockSession);
if (dataContext == null)
Expand All @@ -240,7 +242,7 @@ public static WTMContext CreateWtmContext(IDataContext dataContext= null, string
wtmcontext.LoginUserInfo = new LoginUserInfo { ITCode = usercode ?? "user" };
wtmcontext.GlobaInfo.AllAccessUrls = new List<string>();
wtmcontext.GlobaInfo.AllAssembly = new List<System.Reflection.Assembly>();
wtmcontext.GlobaInfo.AllModule = new List<WalkingTec.Mvvm.Core.Support.Json.SimpleModule>();
wtmcontext.GlobaInfo.AllModule = new List<Core.Support.Json.SimpleModule>();
mockService.Setup(x => x.GetService(typeof(WtmFileProvider))).Returns(new WtmFileProvider(wtmcontext));
return wtmcontext;
}
Expand Down
6 changes: 4 additions & 2 deletions test/WalkingTec.Mvvm.Test.Mock/MockWtmContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ public static WTMContext CreateWtmContext(IDataContext dataContext= null, string
Mock<IServiceProvider> mockService = new Mock<IServiceProvider>();
MockHttpSession mockSession = new MockHttpSession();
mockHttpRequest.Setup(x => x.Cookies).Returns(new MockCookie());
mockService.Setup(x => x.GetService(typeof(IDistributedCache))).Returns(new MemoryDistributedCache(Options.Create<MemoryDistributedCacheOptions>(new MemoryDistributedCacheOptions())));
var cache = new MemoryDistributedCache(Options.Create<MemoryDistributedCacheOptions>(new MemoryDistributedCacheOptions()));
var res = new ResourceManagerStringLocalizerFactory(Options.Create<LocalizationOptions>(new LocalizationOptions { ResourcesPath = "Resources" }), new Microsoft.Extensions.Logging.LoggerFactory());
mockService.Setup(x => x.GetService(typeof(IDistributedCache))).Returns(cache);
mockHttpContext.Setup(x => x.Request).Returns(mockHttpRequest.Object);
mockHttpContext.Setup(x => x.RequestServices).Returns(mockService.Object);
var httpa = new HttpContextAccessor();
httpa.HttpContext = mockHttpContext.Object;
var wtmcontext = new WTMContext(null, new GlobalData(), httpa, new DefaultUIService(), null,dataContext, new ResourceManagerStringLocalizerFactory(Options.Create<LocalizationOptions>(new LocalizationOptions { ResourcesPath = "Resources" }), new Microsoft.Extensions.Logging.LoggerFactory()));
var wtmcontext = new WTMContext(null, new GlobalData(), httpa, new DefaultUIService(), null,dataContext, res, cache:cache);
wtmcontext.MSD = new BasicMSD();
wtmcontext.Session = new SessionServiceProvider(mockSession);
if (dataContext == null)
Expand Down

0 comments on commit 3247a66

Please sign in to comment.