-
-
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
Need a way to setup a property / method by name #580
Comments
Moq already allows you to do what you want: Most (There's one exception, and that's if you want to set up a property set expectation. For example, Adding new overloads for |
Interesting, I hadn't thought of trying to get access that way. I'll see if I can get that working. Thank you for taking the time help. |
Good luck. Report back if you get stuck, and I'll try to demo with a brief example. |
Success (at least it seems to be working)!
Thanks again! |
@BradleyUffner: That's looking good! (Note that this is how |
Just in case anyone else needs this functionality, I made an extension method to make things significantly easier that seems to handle all the cases I threw at it.
It can be used like this (Equivalent of
or this (Equivalent of
I know the format is a little ugly, but it's the best way I could figure out how to make it support both the constant form, and the "It.IsAny" form. There are probably all kinds of improvements that could be made to the code of the extension method. I'm not going to make any claims against its efficiency, or that it won't blow up on some calls I haven't tested, but it works well enough for my uses. |
@BradleyUffner: First of all, thanks for sharing your work! 👍
Let me point you at Moq's in-built mock.Protected().Setup("ProtectedMethod", 1, false, ItExpr.IsAny<string>(), 4.0); (I might be wrong with some details here as I'm citing the algorithm from memory, but that's the basic idea. Take a look at Moq's source code to see how exactly arguments are transformed from |
I'm building a unit test framework to assist with testing code that relies on Entity Framework, and I need a way to setup a callback on a method by name.
The primary function of my project is to dynamically create a mock object that behaves like user's subclass of DbContext. I have a ContextMockBuilder class, where T is the interface representing the DbContext under test. I create a Mock of T, then loop through all the properties on T, using reflection, to find all the properties of type DbSet , and populate them with a customized subclass TestDbSet. This works great, but I need to be able to call custom code for the SaveChanges method. Unfortunately, DbContext doesn't implement an interface with the SaveChanges method on it, which means I can't constraint T. That leads to not being able to use Moq's normal methods to setup the callback, since "mock.Setup(foo => ...)" foo will only contains members of System.Object.
I either need some method to setup a method by name, or some other way to do it that doesn't involve a strongly typed Expression. I thought I would be able to "abuse" Protected, since it allows member access by name, but apparently it throws an error if the member is public (otherwise, I believe it would work).
The text was updated successfully, but these errors were encountered: