November Happy Hour will be moved to Thursday December 5th.
November Happy Hour will be moved to Thursday December 5th.
GetTotal is an extension method, you can't just mock it. What you can do is to use the other overload which takes an IOrderGroupCalculator, and mock its GetTotal instead, something like this
public CartService(IOrderGroupCalculator orderGroupCalculator)
{
_orderGroupCalculator = orderGroupCalculator;
}
then
var total = cart.GetTotal(_orderGroupCalculator ).Amount
Then in your code
var mock = new Mock<IOrderGroupCalculator>();
mock.Setup(m=>m.GetTotal(It.IsAny<IOrderGroup>())).Returns(<money>)
var myCartService = new CartService(mock.Object);
Would there be a way around mocking the GetAllLineItems? It's an extension method, and unlike f.ex. GetTotal, this method doesn't seem to have any overloads. I could of course move the call of GetAllLineItems to a separate service, and then mock that service, but that would be a bit inconvenient.
I'm using Commerce 11.8.3.
You don't have to
var cartMock = new Mock<ICart>();
//mock other stuffs
var formMock = new Mock<IOrderForm>();
cartMock.Setup(c=>c.Forms).Returns(new [] { formMock.Object });
var shipmentMock = new Mock<IShipment>();
formMock.Setup(f => f.Shipments).Returns(new [] { shipmentMock.Object });
var lineitemMock = new Mock<LineItem>();
...
It would be better if you have your fakes like:
public class FakeOrderGroup : IOrderGroup
{
//implement stuffs
}
and so on.
I have this method, I want to unit test with NUnit:
Thus I need to mock the cart:
The question is, how I can mock the extension method GetTotal, since it's in a library I cannot modify?
When I run over the:
I get this runtime error:
System.NotSupportedException: 'Expression references a method that does not belong to the mocked object: x => x.GetTotal()'