public void Initializer() { testMessage1 = new Message() { MessageID = 1, Title = "Test Title", MessageContent = "Test content", DateSent = DateTime.Now.Date, ReceiverUserID = "1", SenderUserID = "2" }; testMessage2 = new Message() { MessageID = 2, Title = "Test2 Title", MessageContent = "Test2 content", DateSent = DateTime.Now.Date, ReceiverUserID = "3", SenderUserID = "4" }; testMessageList = new List <Message>() { testMessage1, testMessage2 }; dbSet = new Mock <DbSet <Message> >(); dbSet.As <IQueryable <Message> >().Setup(q => q.Provider).Returns(() => testMessageList.AsQueryable().Provider); dbSet.As <IQueryable <Message> >().Setup(q => q.Expression).Returns(() => testMessageList.AsQueryable().Expression); dbSet.As <IQueryable <Message> >().Setup(q => q.ElementType).Returns(() => testMessageList.AsQueryable().ElementType); dbSet.As <IQueryable <Message> >().Setup(q => q.GetEnumerator()).Returns(() => testMessageList.AsQueryable().GetEnumerator()); dbSet.Setup(x => x.Add(It.IsAny <Message>())).Callback <Message>(testMessageList.Add); dbSet.Setup(set => set.AddRange(It.IsAny <IEnumerable <Message> >())).Callback <IEnumerable <Message> >(testMessageList.AddRange); dbSet.Setup(set => set.Remove(It.IsAny <Message>())).Callback <Message>(t => testMessageList.Remove(t)); dbSet.Setup(set => set.RemoveRange(It.IsAny <IEnumerable <Message> >())).Callback <IEnumerable <Message> >(ts => { foreach (var t in ts) { testMessageList.Remove(t); } }); mockContext = new Mock <MessageContext>(new DbContextOptions <MessageContext>()); mockContext.Setup(x => x.Messages).Returns(dbSet.Object); var testClaims = new ClaimsPrincipal(new ClaimsIdentity(TokenGenerator.UserToken("Customer").Claims)); messaging = new Messaging(mockContext.Object); apiMessaging = new ApiMessagingLogic(mockContext.Object); ApiController = new MessagesController(mockContext.Object); ApiController.ControllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext { User = testClaims } }; MvcController = new MessagesMVCController(mockContext.Object); MvcController.ControllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext { User = testClaims } }; }
public MessagesController(MessageContext cxt) { context = cxt; messaging = new ApiMessagingLogic(cxt); //cxt.Database.EnsureCreated(); }