Skip to content
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

Unable to call base interface getter with cast without explicit setup. #1254

Closed
gurustron opened this issue Apr 29, 2022 · 4 comments
Closed

Comments

@gurustron
Copy link

gurustron commented Apr 29, 2022

public interface IQueryModel : IQueryModelImmutable
{
    public int Property1 { get; set; } 
}

public interface IQueryModelImmutable
{
    public int Property1 { get; } 
}

Mock<IQueryModel> mock = new Mock<IQueryModel>
{
    // CallBase = true // does not affect things, can be commented in/out
};
// mock.Setup(m => m.Property1).Returns(100);
mock.As<IQueryModelImmutable>().Setup(m => m.Property1).Returns(100); // works only if both setups are present
IQueryModelImmutable toTest = mock.Object;
Assert.AreEqual(100, mock.Object.Property1);
Assert.AreEqual(100, toTest.Property1);

Tried different combinations of setting up only one of interfaces and using/ not using CallBase - only working one is when both interfaces get_Property1 is set up explicitly.

@stakx
Copy link
Contributor

stakx commented Apr 29, 2022

CallBase has no effect in this case because there is no base implementation to call; all your interfaces' members are abstract.

If you implemented those interfaces yourself, you wouldn't try to invoke base.Property1, right?

@stakx stakx closed this as completed Apr 29, 2022
@gurustron
Copy link
Author

gurustron commented Apr 30, 2022

@stakx it's not about CallBase. If you remove it still setting up both methods is needed. Casting to base interface is not working otherwise.

If I've implemented both those interfaces in one class - casting it to base interface would have worked.

@gurustron
Copy link
Author

@stakx ?

@stakx
Copy link
Contributor

stakx commented May 3, 2022

I don't get it. If it' not about CallBase, why is it mentioned everywhere, including in your question title? I suggest you rephrase the whole thing and remove whatever isn't actually related to your issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants