public async void AddStockAsync_ReturnsCorrectResult()
        {
            var repo = new Mock <IStockRepository>();

            repo.Setup(x => x.AddAsync(It.IsAny <Stock>())).Callback((Stock newStock) => {
                if (fakeStocks.Any(s => s.Code == newStock.Code))
                {
                    throw new AlreadyInDbException();
                }
            });

            var service = new StockService(repo.Object);

            Stock newStock = new Stock("Zzz Stock Company", "ZSC", 86, 82, "NASDAQ", false);

            await service.AddStockAsync(newStock);

            repo.Verify(t => t.AddAsync(It.IsAny <Stock>()), Times.Once());

            Stock oldStock = new Stock("Tian Stock Company", "TSC", 84, 80, "NASDAQ", false);

            try
            {
                await service.AddStockAsync(oldStock);
            }
            catch (Exception ex)
            {
                Assert.Equal("AlreadyInDbException", ex.GetType().Name);
                repo.Verify(t => t.AddAsync(It.IsAny <Stock>()), Times.Exactly(2));
            }
        }
Пример #2
0
        public void AddStockAsync_add_stock_type_bond_correct()
        {
            Stock stock = new Stock {
                Type = StockType.Bond
            };

            _stockRepositoryFake.Setup(x => x.GetTotalCount(stock.Type)).Returns(0).Verifiable();
            _stockRepositoryFake.Setup(x => x.Add(It.Is <Stock>(y => y.Type == StockType.Bond))).Verifiable();

            _stockService.AddStockAsync(stock).GetAwaiter().GetResult();
        }