Testing get / set with Moq

I am trying to check a set of properties, but I do not know how to do this in this particular case. I am using .NET 4.5 and the Moq framework.

I have this method:

 public Invoice Approve(Invoice invoiceToBeApproved)
{
    var approvedInvoiceStatus = InvoiceStatusRepository.First<ApprovedStatus>();

    invoiceToBeApproved.Car.RemainingSeats -= 1;

    if (invoiceToBeApproved.Car.RemainingSeats < 0)
    {
        throw new BusinessException("There are no remaining seats");
    }

    invoiceToBeApproved.InvoiceStatus = approvedInvoiceStatus;

    InvoiceRepository.Update(invoiceToBeApproved);

    context.SaveChanges();

    return invoiceToBeApproved;
}

I am doing a unit test (not an integration test, so I am not going to db).

What I want to check

invoiceToBeApproved.Car.RemainingSeats -= 1;

I tried using VerifyGet, but I just can't reach this property.

Part of my test is

var newInvoice = InvoiceService.ApproveInvoice(invoice);
InvoiceStatusRepository.Verify(x => x.First<ApprovedStatus>());
InvoiceRepository.Verify(x => x.Update(It.IsAny<Invoice>()));
context.Verify(x => x.SaveChanges(), Times.Exactly(1));

How can I check the kit / get RemainingSeats?

Thanks in advance!

+4
source share
2 answers

I do not think you should use Moq at all for this. Assuming what Invoiceis POCO, just confirm the value of the property.

+1
source

, Moq . , .

public class BusinessLogicClass
{
    public void MethodUnderTest(IFoo foo)
    {
        foo.Bar -= 1;
    }
}

public interface IFoo
{
    int Bar { get; set; }
}

public class Foo : IFoo
{
    public int Bar { get; set; }
}

[TestMethod]
public void PropertyTest()
{
    const int expected = 41;
    var foo = new Foo { Bar = 42 };
    var sut = new BusinessLogicClass();
    sut.MethodUnderTest(foo);
    Assert.AreEqual(expected, foo.Bar);
}

[TestMethod]
public void PropertyTestWithMoq()
{
    var fooMoq = new Mock<IFoo>();
    var sut = new BusinessLogicClass();
    sut.MethodUnderTest(fooMoq.Object);
    fooMoq.Verify(x => x.Bar, Times.Once);
}
0

All Articles