public void Initialize() { // Since the repository returns Broker entities but the service returns BrokerDTO entities we need to initialize Automapper AutomapperBootstrapper.Initialize(); // Initialize the data we're going to use in the tests _propertyListMock = PropertyMocks.GetPropertyListMock(); // Get the DTO's by using the moked list _propertyListItemDtoMock = Mapper.Map <List <Property>, List <PropertyListItemDTO> >(_propertyListMock); _propertyDtoMock = Mapper.Map <Property, PropertyDTO>(_propertyListMock.ElementAt(0)); _propertyRepositoryMock = new Mock <IPropertyRepository>(); // Initialize the mocked repository _propertyRepositoryMock.Setup(repository => repository.GetAll()) .ReturnsAsync(_propertyListMock); _propertyRepositoryMock.Setup(repository => repository.Get(1)) .ReturnsAsync(_propertyListMock.ElementAt(0)); _propertyRepositoryMock.Setup(repository => repository.Get(9)) .ReturnsAsync(null); // Use the mock to create an instance of the service _propertyServiceMock = new PropertyService(_propertyRepositoryMock.Object); }
public void Initialize() { // Initialize the data we're going to use in the tests _propertyListMock = PropertyMocks.GetPropertyListMock(); // First we neet to mock the DbSet _propertyDbSetMock = new Mock <DbSet <Property> >(); // Since we're faking the db context with async methods, we need to set up a few things _propertyDbSetMock.As <IDbAsyncEnumerable <Property> >() .Setup(m => m.GetAsyncEnumerator()) .Returns(new TestDbAsyncEnumerator <Property>(_propertyListMock.AsQueryable().GetEnumerator())); _propertyDbSetMock.As <IQueryable <Property> >() .Setup(m => m.Provider) .Returns(new TestDbAsyncQueryProvider <Property>(_propertyListMock.AsQueryable().Provider)); _propertyDbSetMock.As <IQueryable <Property> >().Setup(m => m.Expression).Returns(_propertyListMock.AsQueryable().Expression); _propertyDbSetMock.As <IQueryable <Property> >().Setup(m => m.ElementType).Returns(_propertyListMock.AsQueryable().ElementType); _propertyDbSetMock.As <IQueryable <Property> >().Setup(m => m.GetEnumerator()).Returns(_propertyListMock.AsQueryable().GetEnumerator()); // Now we can mock the entire context _realStateContextMock = new Mock <IRealStateContext>(); _realStateContextMock.Setup(context => context.Properties).Returns(_propertyDbSetMock.Object); // Initialize the repository with the mocked dbContext _propertyRepositoryMock = new PropertyRepository(_realStateContextMock.Object); }