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
I recently updated a project from Moq 4.0.10827 to the current version (4.5.29). This caused some tests to fail. I eventually tracked down the issue to the fact that a non-virtual method that implements an interface now seems to be treated as mockable when called through the interface. From what I can tell, this behavior began in version 4.2.1408.0511. Was this change intentional? Is it documented somewhere? I didn't see anything about it in the 4.2 release notes.
Here's the code to reproduce:
void Main()
{
var m = new Mock<Foo>();
Console.WriteLine(m.Object.GetValue()); // 10
Console.WriteLine(((IFoo)m.Object).GetValue()); // 10 in older versions, 0 in newer versions
}
// Define other methods and classes here
public class Foo : IFoo
{
public int GetValue() => 10;
}
public interface IFoo
{
int GetValue();
}
I'm not really sure whether I consider the new behavior better or worse. It's definitely a bit strange to have the same method returning a different value when called through the interface when there's no explicit implementation happening in the code. On the other hand, I could see how this could enable some helpful scenarios, and the issue can always be worked around by setting CallBase to true. Assuming it is intentional and desired, maybe we could add something to the release notes to call this out?
The text was updated successfully, but these errors were encountered:
I recently updated a project from Moq 4.0.10827 to the current version (4.5.29). This caused some tests to fail. I eventually tracked down the issue to the fact that a non-virtual method that implements an interface now seems to be treated as mockable when called through the interface. From what I can tell, this behavior began in version 4.2.1408.0511. Was this change intentional? Is it documented somewhere? I didn't see anything about it in the 4.2 release notes.
Here's the code to reproduce:
I'm not really sure whether I consider the new behavior better or worse. It's definitely a bit strange to have the same method returning a different value when called through the interface when there's no explicit implementation happening in the code. On the other hand, I could see how this could enable some helpful scenarios, and the issue can always be worked around by setting CallBase to true. Assuming it is intentional and desired, maybe we could add something to the release notes to call this out?
The text was updated successfully, but these errors were encountered: