How to test this?
public void TestMethod()
{
if(aDenpendency.someMethod()) return;
// Followed by other logic....
}
I don’t have another dependency in the following logic, so I couldn’t use AssertWasNotCalled.
Thanks in advance!!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It is a mistake to test as you are describing. And denotes poor code structure and is a definate code smell, you should refactor and unit test that code, Given that you aim to test it has not been called it follows you also want a test when it has been called.
You need to have a method have a single responsibility,therefore for more sensible unit testing you can take out the logic after the return encase it in a method and unit test the output of that method.
A unit test is not there to test what was not called it is there to either test an expectation setup by a mock/fake or you should test the result of the operation.
You could write a test that tests the state of the object once the method has been called but this is really an integration test rather than a unit test. That means you can test the state that is as should be after the return statement has executed the other code will not have executed so whatever that modifies or processes will not have been modified.