Context MakeContext() { var context = new Context(); var sessionMock = new Mock <ISession>(); sessionMock.Setup(m => m.Set(It.IsAny <string>(), It.IsAny <byte[]>())) .Callback((string key, byte[] value) => { context.SessionObjects.Add(key, value); }); var tryGetValueCallback = new Context.TryGetValueCallback((string key, out byte[] value) => { value = context.SessionObjects[key]; }); byte[] dummy; sessionMock.Setup(m => m.TryGetValue(It.IsAny <string>(), out dummy)) .Callback(tryGetValueCallback) .Returns(true); context.Session = sessionMock.Object; return(context); }
Context MakeContenxt( bool isAuthenticated = false, User loggedInUser = null, List <Branch> branches = null, List <Car> cars = null) { var context = new Context { IsAuthenticated = isAuthenticated, LoggedInUser = loggedInUser, Branches = branches, Cars = cars }; var sessionMock = new Mock <ISession>(); sessionMock.Setup(m => m.Set(It.IsAny <string>(), It.IsAny <byte[]>())) .Callback((string key, byte[] value) => { context.SessionObjects[key] = value; }); var tryGetValueCallback = new Context.TryGetValueCallback((string key, out byte[] value) => { context.SessionObjects.TryGetValue(key, out value); }); byte[] dummy; sessionMock.Setup(m => m.TryGetValue(It.IsAny <string>(), out dummy)) .Callback(tryGetValueCallback) .Returns(true); var httpContextAccessorMock = new Mock <IHttpContextAccessor>(); httpContextAccessorMock.Setup(m => m.HttpContext.User.Identity.IsAuthenticated) .Returns(context.IsAuthenticated); httpContextAccessorMock.Setup(m => m.HttpContext.User.Identity.Name) .Returns(context.LoggedInUser?.UserName); httpContextAccessorMock.Setup(m => m.HttpContext.Session) .Returns(sessionMock.Object); context.HttpContextAccessor = httpContextAccessorMock.Object; var usersDaoMock = new Mock <IUsersDao>(); usersDaoMock.Setup(m => m.GetByUserNameWithRoleAsync(It.IsAny <string>())) .ReturnsAsync(loggedInUser); context.UsersDao = usersDaoMock.Object; var branchesDaoMock = new Mock <IBranchesDao>(); branchesDaoMock.Setup(m => m.GetByIdAsync(It.IsAny <long>())) .ReturnsAsync((long id) => context.Branches.First(b => b.Id == id)); context.BranchesDao = branchesDaoMock.Object; var carsDaoMock = new Mock <ICarsDao>(); carsDaoMock.Setup(m => m.GetByIdAsync(It.IsAny <long>())) .ReturnsAsync((long id) => context.Cars.First(c => c.Id == id)); context.CarsDao = carsDaoMock.Object; return(context); }