-
-
Notifications
You must be signed in to change notification settings - Fork 811
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
[Feature Request] Allow to change MockBehavior of mocks #1230
Comments
Without knowing all the details of your exact testing scenario, why don't you simply use different mocks for the different test cases, some being strict, and some being loose? If the issue is having to set up several mocks almost identically, that should be easy enough to solve through a mock factory method having a |
I'm using AutoMocker to populate complex objects with mocks. It has a constructor parameter |
So if I understand correctly, the source of your problem is that AutoMocker only gives you the choice between loose and strict mocks once, when you'd really need to choose between those two behaviors at a more granular level? In that case, it seems you should make your request with AutoMocker instead... solve the problem at the source, instead of asking for a workaround further downstream. |
@stakx , do you remember our discussion about mixed mode: strict for queries, loose for commands? ;-) I guess, @alex-fomin , needs something similar. |
It's quite not the same. My example looks like class ObjectUnderTest
{
public ObjectUnderTest(IDependecy1 d1, IDependecy2 d2)
{
// ...
}
public void MethodToTest(int arg)
{
d1.NotImportantCall(...);
d2.ImportantCall(...);
}
}
public void Test()
{
var mocker = new AutoMocker(MockBehavior.Strict);
mocker.GetMock<IDependecy2>()
.Setup(x => x.ImportantCall(...)) // <- I want to be specific that IDependecy2 instance will be called as defined
...; // And neither additional calls, nor call with other parameters
// I don't care about IDependecy2 calls at all
mocker.GetMock<IDependency1>().Behavior = MockBehavior.Loose; // <- but I can't
// But I will do care about new dependencies in MethodToTest in future
// So I create AutoMocker with MockBehavior.Strict
// So the test will fail in future after someone changes MethodToTest
var service = mocker.Create<ObjectUnderTest>();
service.MethodToTest(1);
...
} |
But why not call Also, I suspect the method you don't care about either has command semantics, or it's output not being checked by the code which uses that value as an input. |
In simple cases |
This still strikes me as a primarily AutoMocker-specific issue. Instead of changing Moq such that one can arbitrarily change an existing Or, at least, AutoMocker, being the downstream library, should be the first to allow reconfiguring its While changing Moq as requested would be technically possible, I think it's the wrong approach to solve the issue at hand, and finally, I don't think it would be a particularly good enhancement; it could also make future maintenance more difficult. |
(P.S.: removing the |
Another case I am right now in that and I think it could be a valid scenario for the feature request. using Xunit, with Autofixture and automoq. Lets say the test is something like this: ` // ILogger is the logging interface from base using Microsoft.Extensions.Logging; ` ILogger interface is very basic and it has some extensions from Microsoft. Lets say this configuration is something like this: ` public static void ConfigureLogger(Mock<ILogger> logger, ITestOutputHelper outputHelper) ` As logger behaviour cannot be set, this way is not possible. My only approach so far is to create the mock manually or to force autofixture to create all mocks with strict behaviour. Neither of both are ideal. |
@dilandau2001 , Why not just create custom |
@voroninp |
@dilandau2001 , custom resolver? Here's a similar issue |
mmm, I wasn't using Moq.AutoMocker. I feel that your suggestion should work somehow... although I don't know yet how I would add that resolver to the autofixture resolve pipeline. |
Just for the sake of continuation if someone falls into this post. As it is kind of mix responsability among Autofixture, Xunit and Moq libraries I kind of feel the Autofixture github is the appropiate one. |
This feature is especially useful with mix of AutoMocker. For instance, I have a complex class to test, so I setup a AutoMocker with Loose behavior and set all required dependencies (leaving all unimportant services as Loose mocks). But then I need to verify that particular service should be mocked as Strict to ensure that my test setups all required methods of the service. At the moment, I have to turn AutoMocker to Strict mode all together and provide all mocks with default behavior even if I'm not interested in them.
It would be cool to change behavior of mocks on the fly by changing its Behavior property.
The text was updated successfully, but these errors were encountered: