public void Should_Get_All() { IQueryable <Debt> debts = new List <Debt> { new Debt { Id = 1, Sum = 15, TakerId = currentUser.Id }, new Debt { Id = 2, Sum = 20, GiverId = currentUser.Id } }.AsQueryable(); var mockSet = new Mock <DbSet <Debt> >(); mockSet.As <IQueryable <Debt> >().Setup(m => m.Provider).Returns(debts.Provider); mockSet.As <IQueryable <Debt> >().Setup(m => m.Expression).Returns(debts.Expression); mockSet.As <IQueryable <Debt> >().Setup(m => m.ElementType).Returns(debts.ElementType); mockSet.As <IQueryable <Debt> >().Setup(m => m.GetEnumerator()).Returns(debts.GetEnumerator()); var mockContext = new Mock <DatabaseContext>(); mockContext.Setup(c => c.Debts).Returns(mockSet.Object); var service = new DebtsService(mockContext.Object, iMapper, mockAccessor.Object, new DebtDtoMapper(iMapper)); var noFilter = new DebtFilterDto { RoleInDebt = null }; var actual = service.GetAll(noFilter); Assert.Equal(2, actual.Count()); mockContext.Verify(m => m.Debts); Assert.Equal(debts.First(d => d.Id == 1).Sum, actual.First(d => d.Id == 1).Sum); Assert.Equal(debts.First(d => d.Id == 2).Sum, actual.First(d => d.Id == 2).Sum); }
public void Should_Create() { Debt myDebt = new Debt { Sum = 15, GiverId = currentUser.Id, TakerId = 2 }; var mockContext = new Mock <DatabaseContext>(); mockContext.Setup(c => c.Debts.Add(myDebt)); var service = new DebtsService(mockContext.Object, iMapper, mockAccessor.Object, new DebtDtoMapper(iMapper)); service.CreateDebt( new DebtInboxDto() { GiverId = (int)myDebt.GiverId, TakerId = (int)myDebt.TakerId, Sum = myDebt.Sum } ); mockContext.Verify(m => m.SaveChanges()); mockContext.Verify(m => m.Debts.Add(It.Is <Debt>(t => t.Sum == myDebt.Sum && t.TakerId == myDebt.TakerId && t.GiverId == myDebt.GiverId && t.IsActive == true && t.Date.Date == DateTime.Now.Date && t.Deadline == t.Deadline && t.Description == t.Description ))); }
public DebtsController( DebtsService debtsService, ICurrentUserAccessor currentUser, IMapper mapper) { _debtsService = debtsService; _currentUser = currentUser; _mapper = mapper; }