Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
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:
var mock_Cart = new Mock();
mock_Cart.Setup(x => x.GetTotal()).Returns(new Mediachase.Commerce.Money(123, Currency.ARS)); // <-- this fails because gettotal is an extension method _mock_myservice.setupx=""> x.MethodToLookupCart()).Returns(_mock_Cart.Object);
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()'