You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When I'm trying to mock a class that derives from another (none virtual) class the unit test always passes.
Lets look at an example where I think the bug can be seen clearly:
• We have two classes. Class B which has a virtual method (with the name AMethod() and ...
• Class A, which derives from B and overrides its virtual method.
• Both methods (so the virtual class in B and the method in A) throw an exception.
When we now create a unit test that tests the method AMethod in A, the test should fail, since AMethod throws an exception.
But as you can see the test passes:
Now I think this might be a bug, because when I convert B to an interface the method failes as expected.
As you can see the unit test for the method AMethod in the class A fails.
One more thing to note is that when B was a class and not an interface, setting a breakpoint in the methods in A or B did not work, so I'm assuming the method in A nor B are never actually called?
The text was updated successfully, but these errors were encountered:
Moq by default intercepts / stubs all methods that it can (that is, all overridable ones), which is why your original implementation gets overridden with one that doesn't really do anything (unless you set it up). So your throw no longer executes.
why does this property (CallBase) exist?
Because sometimes you don't want a full stub, but you may want to defer to the implementation from the mocked type (which yields a so-called "partial mock"). You can choose that as the default for all methods using mock.CallBase = true, or for single methods using mock.Setup(m => m.Method(...)).CallBase().
when I convert B to an interface the method failes as expected
Not only did you convert B to an interface, you also made A.AMethod non-virtual, so Moq cannot intercept it, which is why the original implementation will get called.
When I'm trying to mock a class that derives from another (none virtual) class the unit test always passes.
Lets look at an example where I think the bug can be seen clearly:
• We have two classes. Class B which has a virtual method (with the name AMethod() and ...
• Class A, which derives from B and overrides its virtual method.
• Both methods (so the virtual class in B and the method in A) throw an exception.
When we now create a unit test that tests the method AMethod in A, the test should fail, since AMethod throws an exception.
But as you can see the test passes:
Now I think this might be a bug, because when I convert B to an interface the method failes as expected.
As you can see the unit test for the method AMethod in the class A fails.
One more thing to note is that when B was a class and not an interface, setting a breakpoint in the methods in A or B did not work, so I'm assuming the method in A nor B are never actually called?
The text was updated successfully, but these errors were encountered: