1

let's say I have this class

public class MyClass
{
    public void InnerMethod(){}

    public void OuterMethod(SomeType sameValue)
    {
       if(someCondition)
       {
          InnerMethod(); 
       }
       else
       {
         ...
       }
    }
}

I want to check that the InnerMethod was called

[Fact]
public void CanDoSomething()
{
   var sut = new MyClass();
   sut.OuterMethod(someValue);

  //Assert that InnerMethod was called
}

If InnerMethod belonged to a mocked object, I would have just called mockObject.Verify(x => x.InnerMethod);. However, InnerMethod is a concrete method belonging to the sut itself.

How to check that InnerMethod was called.

Thanks for helping.

Stam
  • 2,410
  • 6
  • 32
  • 58
Richard77
  • 20,343
  • 46
  • 150
  • 252
  • There is a philosophy for unit testing that only observable public behavior should be tested. The method `InnerMethod` is indeed public, but whether it is called by some other method of the class is an inner implementation detail. What changes in the object or its environment after calling `InnerMethod` ? (e.g. a property changes, an entry is made in database, ...). Those are the observable public effects that would be suitable to check in your test. – Christopher Hamkins Mar 30 '22 at 09:33
  • Similar question asked just now as well https://stackoverflow.com/questions/71668511/how-to-verify-with-xunit-that-method-was-called-or-was-not-called-without-moq – Christopher Hamkins Mar 30 '22 at 10:37

0 Answers0