public void Initialize() { // Initialize the data we're going to use in the tests _brokerListMock = BrokerMocks.GetBrokerListMock(); // First we need to mock the DbSet _brokerDbSetMock = new Mock <DbSet <Broker> >(); // Since we're faking the db context with async methods, we need to set up a few things first _brokerDbSetMock.As <IDbAsyncEnumerable <Broker> >() .Setup(m => m.GetAsyncEnumerator()) .Returns(new TestDbAsyncEnumerator <Broker>(_brokerListMock.AsQueryable().GetEnumerator())); _brokerDbSetMock.As <IQueryable <Broker> >() .Setup(m => m.Provider) .Returns(new TestDbAsyncQueryProvider <Broker>(_brokerListMock.AsQueryable().Provider)); _brokerDbSetMock.As <IQueryable <Broker> >().Setup(m => m.Expression).Returns(_brokerListMock.AsQueryable().Expression); _brokerDbSetMock.As <IQueryable <Broker> >().Setup(m => m.ElementType).Returns(_brokerListMock.AsQueryable().ElementType); _brokerDbSetMock.As <IQueryable <Broker> >().Setup(m => m.GetEnumerator()).Returns(_brokerListMock.AsQueryable().GetEnumerator()); // Now we can mock the entire context _realStateContextMock = new Mock <IRealStateContext>(); _realStateContextMock.Setup(context => context.Brokers).Returns(_brokerDbSetMock.Object); // Initialize the repository with the mocked dbContext _brokerRepositoryMock = new BrokerRepository(_realStateContextMock.Object); }
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 _brokerDTOListMock = Mapper.Map <List <Broker>, List <BrokerDTO> >(BrokerMocks.GetBrokerListMock()); _brokerServiceMock = new Mock <IBrokerService>(); // Initialize the mocked service _brokerServiceMock.Setup(service => service.GetAll()) .ReturnsAsync(_brokerDTOListMock); _brokerServiceMock.Setup(service => service.Get(1)) .ReturnsAsync(_brokerDTOListMock.ElementAt(0)); _brokerServiceMock.Setup(service => service.Get(9)) .ReturnsAsync(null); // Use the mock to create an instance of the service _brokerControllerMock = new BrokersController(_brokerServiceMock.Object); }
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 _brokerListMock = BrokerMocks.GetBrokerListMock(); // Get the list of DTO's by using the moked list _brokerListDtoMock = Mapper.Map <List <Broker>, List <BrokerDTO> >(_brokerListMock); _brokerRepositoryMock = new Mock <IBrokerRepository>(); // Initialize the mocked repository _brokerRepositoryMock.Setup(repository => repository.GetAll()) .ReturnsAsync(_brokerListMock); _brokerRepositoryMock.Setup(repository => repository.Get(1)) .ReturnsAsync(_brokerListMock.ElementAt(0)); _brokerRepositoryMock.Setup(repository => repository.Get(9)) .ReturnsAsync(null); // Use the mock to create an instance of the service _brokerServiceMock = new BrokerService(_brokerRepositoryMock.Object); }