Reputation: 179
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.
Upvotes: 1
Views: 3467
Reputation: 12523
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.
Upvotes: 3
Reputation: 311808
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());
Upvotes: 3