1

I am working on Unit Test cases for my class, and I have one scenario for which I need to write Unit Test.

 public void CheckModelDetail(ProductModel model)
 {
       if (executeTemodel == null)
       {
            return;
       }

       checkModel();
 }

So I am not sure how I can check for return statement.

4
  • 1
    In the test case, you should make sure when executeTemodel is null checkModel is not called. It can be done using mock frameworks or by creating a dummy class deriving from your business class and overriding some of the methods or setting a flag. Commented Dec 29, 2018 at 10:12
  • When you ‘return’ your code is expecting a value of something to output. Your above code isn’t returning anything. If you just wish to break out of the if block the. Use ‘break’. However if you are going to return something change your method from ‘void’ Commented Dec 29, 2018 at 10:13
  • @RezaAghaei, that is Okay. I have to just write a Test case for that if condition which is returning "return". And other things are handling in other test cases. Commented Dec 29, 2018 at 10:14
  • what does checkModel do? I assume it checks wether the model is valid or something but then what? Does it thrown an exception or something? Is it a public method or private? Commented Dec 29, 2018 at 10:14

2 Answers 2

3

One possible appraoch could be to mock the class, call CheckModelDetail with null and verify that checkModel was never called. E.g., using Moq:

var mock = new Mock<MyClass> { CallBase = true };
mock.CheckModelDetail(null);
mock.Verify(m => m.checkModel(), Times.Never());
Sign up to request clarification or add additional context in comments.

Comments

3

The buzzword here (with plenty of literature out there) is Observable Effect. Usually it's either a state change, a return value or an exception that is thrown (or not).

If, for instance, you want to test that no exception is thrown (Observable Effect), then you need not assert on any output:

public void ShouldNotThrowException()
{
    var objectUnderTest = new YourClass();
    objectUnderTest.CheckModelDetail(null);
}

Try to remove your if (executeTemodel == null) clause and you will see that your test fails somewhere in checkModel (presuming you access members of your model in there). Any unhandled exception makes your test fail.

You could use Shouldly's Should.NotThrow to make this even more explicit:

public void ShouldNotThrowException()
{
    var objectUnderTest = new YourClass();
    Should.NotThrow(() => objectUnderTest.CheckModelDetail(null));
}

This not only tests your code, but also makes is very clear to whoever reads your test code what you expect.

Should you want to assert that checkModel() is not called, you should move this method into a different class and work with a Mock (see Martin Fowler's TestDouble for a nice overview). But I'm not sure whether you care about that as the consumer of CheckModelDetail. You should test what you want to happen (i.e. no exception thrown, model accepted as valid, model considered invalid, ...) as opposed to how it's achieved (i.e. checkModel is not called) in general, so you're free to refactor the implementation as long as the interface does not change.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.