-
-
Notifications
You must be signed in to change notification settings - Fork 794
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Heisenberg's virtual property set in ctor with MockBehavior.Strict and VerifyAll #456
Comments
For further analysis, here's a standalone repro (independent of ASP.NET Core) for the same issue: public interface ILogger { }
public class UserManager
{
public UserManager(ILogger logger)
{
this.Logger = logger;
}
public virtual ILogger Logger { get; set; }
}
[Fact]
public void DamnedIfIDo()
{
var mockUserManager = new Mock<UserManager>(MockBehavior.Strict, null);
mockUserManager.SetupSet(um => um.Logger = null);
var userManager = mockUserManager.Object;
mockUserManager.VerifyAll();
} |
Thanks for reporting this issue. This is something of a current limitation / bug in Moq, and it's caused by good old
Fixing this bug (and many others of a similar kind, see the issue list in the issue referenced above) is going to be hard work and can't be done on the very short term. @dfederm, for the moment, if you, for whatever reason, cannot change var mockUserStore = new Mock<IUserStore<ApplicationUser>>(MockBehavior.Strict);
var mockUserManager = new Mock<UserManager<ApplicationUser>>(
MockBehavior.Strict,
…);
mockUserManager.SetupProperty(_ => _.Logger); // instead of SetupSet; allows the ctor to set Logger
var userManager = mockUserManager.Object;
userManager.Logger = userManager.Logger; // pointless, but satisfies the above setup
mockUserManager.VerifyAll(); |
Thanks! That workaround seems a little better than giving up either |
Closing this, this problem will be tracked in #414 (together with a couple other related issues). |
I'm trying to mock AspNetCore's
UserManager<T>
for a unit test and it seems to be exhibiting some strange behavior.It sets the virtual
Logger
property in its ctor (isn't this not recommended by MSFT?). When neglecting to mock theLogger
setter, I get the following error when constructing the object, as expected:However, if I mock the setter, I get an error when calling
VerifyAll
:So when I mock it, it says it's never called, and if I don't mock it, it gets called!
Code:
The text was updated successfully, but these errors were encountered: