UnitTest - used when testing a method coming from mock

I have a class using an interface that has a void method similar to this witch, now a fully functional method:

public void SellGivenQuantityOfProduct(Product product, int quantity)
{
    product.StockQuantity = product.StockQuantity - quantity;
}

But, working in the TDD way, I started to do it as shown below, but the calculation never happens in my test method

    [TestMethod]
    public void Can_Sell_Given_Quantity_Of_Single_Products_Stock_Quantity()
    {
        var mock = new Mock<ISellBuyPipeLine>();
        mock.Setup(o => o.Products).Returns(new Product[]
        {
            new Product() {ArticleNr = 87, StockQuantity = 10}
        });
        var product = mock.Object.Products.First();
        var choosenProductToSell = new SellBuyProduct() {Product = product, Quantity = 5};
        mock.Object.SellGivenQuantityOfProduct(product, choosenProductToSell.Quantity);

        /*var test = new SellBuyPipeLine();
        test.SellGivenQuantityOfProduct(product, choosenProductToSell.Quantity);*/

        int expectedSellValue = product.StockQuantity;

        Assert.AreEqual(5, expectedSellValue);
    }

If I use outcommented lines and fall into a class containing my method, it works right! But I want to use a layout.

What am I doing wrong?

Am I using the wrong approach?

+4
source share
2 answers

Product -, . , , .

[TestMethod]
public void Can_Sell_Given_Quantity_Of_Single_Products_Stock_Quantity()
{
    var product = new Product { ArticleNr = 87, StockQuantity = 10 };

    var pipeline = new SellBuyPipeLine();
    pipeline.SellGivenQuantityOfProduct(product, 5);

    Assert.AreEqual(5, product.StockQuantity);
}

() , , .

, , .

+2

, . mock object .

, Product, :

public void Can_Sell_Given_Quantity_Of_Single_Products_Stock_Quantity()
{
    // Arrange
    int stockQuantity = 10;
    int quantityToSell = 4;
    int newQuantity = 6;

    var productMock = new Mock<Product>(MockBehavior.Strict);
    productMock.SetupGet(p => p.StockQuantity).Returns(stockQuantity);
    productMock.SetupSet(p => p.StockQuantity = newQuantity);

    var classUnderTest = new SellBuyPipeLine();

    // Act
    test.SellGivenQuantityOfProduct(product, quantityToSell);

    // Assert
    productMock.VerifySet(p => p.StockQuantity, Times.Once());
}

, , , new Product StockQuantity newQuantity;

+3

All Articles