I have a method called Buy, when called it does the following: It calls a factory to create an orderCalls pay on a payment objectCalls empty on the customer's cartCalls the dao to save the orderThis is what my sequence diagram says should happen, however, when I write my unit test for testing that it calls Empty on the cart, it fails (null pointers) unless I specify that the factory returns an order.So I end up faking behavior that isn't relevant, just to test if Empty has been called on the cart.Is this a recognised code smell? When you recognise this in your code, do you change it? I could obviously split up the empty cart functionality, but I do like the clean API of Buy. Not to mention that splitting it up requires Empty to be called first or last, which in this case isn't a problem, but in some situations might be.This clutters my tests quite a bit, and it definitely smells like my method does more than it should, so any pointers to clean solutions would be appreciated. //Implementationpublic void Buy(){    var order = CreateOrder();    Pay();    EmptyCart();    SaveChanges(order);}//Test[TestMethod]public void Buy_WhenCalled_CallsEmptyOnCart(){    var cart = CreateFakeCartThatExpectsCallToEmpty();    var order = CreateFakeOrder().Object;    var orderFactory = CreateFakeOrderFactoryThatReturnsOrder(order).Object;    Cashier cashier = new Cashier(orderFactory, cart);    cashier.Buy();    cart.Verify();}
Is it bad practice, if I have to fake something that is not relevant to my test?
c#;unit testing
No it is not a code smell. Only if you needed to fake dependencies which are unrelated to the specified behavior of the method under test, then it would be a code smell. But it a part of the specified behavior that the Buy()-method causes an order to be created, so you would expect to supply either a real order or a fake. Actually if the method didn't fail on receiving a null instead of an order, it could be considered a code smell.I understand you only want to test a particular part of the methods behavior, but you should still expect the method to do everything it is supposed to do.