public async Task PutUserStock_must_update_userstock_in_context() { var mockSet = new Mock<DbSet<UserStock>>(); mockSet.Setup(m => m.FindAsync(0)) .Returns(Task.FromResult(new UserStock {ID = 0, Name = "DAX", User = "******"})); var mockContext = new Mock<IStockExchangeDbContext>(); mockContext.Setup(m => m.UserStocks).Returns(mockSet.Object); var service = new UserStocksController(mockContext.Object); var result = await service.PutUserStock(0, new UserStock { ID = 0, Name = "DAX", User = "******" }); var statusCodeResult = result as StatusCodeResult; Assert.IsNotNull(statusCodeResult); Assert.AreEqual(HttpStatusCode.NoContent, statusCodeResult.StatusCode); mockContext.Verify(m => m.MarkAsModified(It.IsAny<UserStock>()), Times.Once()); mockContext.Verify(m => m.SaveChangesAsync(), Times.Once()); }
public async Task PutUserStock_must_return_NotFound_when_userstock_does_not_exist_in_dbset() { var data = new List<UserStock>().AsQueryable(); var mockSet = new Mock<DbSet<UserStock>>(); mockSet.As<IQueryable<UserStock>>().Setup(m => m.Provider).Returns(data.Provider); mockSet.As<IQueryable<UserStock>>().Setup(m => m.Expression).Returns(data.Expression); mockSet.As<IQueryable<UserStock>>().Setup(m => m.ElementType).Returns(data.ElementType); mockSet.As<IQueryable<UserStock>>().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator()); var mockContext = new Mock<IStockExchangeDbContext>(); mockContext.Setup(m => m.UserStocks).Returns(mockSet.Object); mockContext.Setup(m => m.SaveChangesAsync()).Throws(new DbUpdateConcurrencyException()); var service = new UserStocksController(mockContext.Object); var result = await service.PutUserStock(0, new UserStock { ID = 0, Name = "DAX", User = "******" }); var notFoundResult = result as NotFoundResult; Assert.IsNotNull(notFoundResult); }