public static T CreateApi <T>(IDataContext dataContext, string usercode) where T : BaseApiController, new() { var _controller = new T(); _controller.Wtm = MockWtmContext.CreateWtmContext(dataContext, usercode); Mock <HttpContext> mockHttpContext = new Mock <HttpContext>(); MockHttpSession mockSession = new MockHttpSession(); mockHttpContext.Setup(s => s.Session).Returns(mockSession); mockHttpContext.Setup(x => x.Request).Returns(new DefaultHttpContext().Request); _controller.ControllerContext.HttpContext = mockHttpContext.Object; _controller.Wtm.MSD = new ModelStateServiceProvider(_controller.ModelState); return(_controller); }
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>(); 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()); 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, res, cache: cache); wtmcontext.MSD = new BasicMSD(); wtmcontext.Session = new SessionServiceProvider(mockSession); if (dataContext == null) { wtmcontext.DC = new EmptyContext(Guid.NewGuid().ToString(), DBTypeEnum.Memory); } else { wtmcontext.DC = dataContext; } 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>(); mockService.Setup(x => x.GetService(typeof(WtmFileProvider))).Returns(new WtmFileProvider(wtmcontext)); return(wtmcontext); }