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

Recursive/Deep Mock setup #597

Closed
informatorius opened this issue Feb 27, 2018 · 5 comments
Closed

Recursive/Deep Mock setup #597

informatorius opened this issue Feb 27, 2018 · 5 comments

Comments

@informatorius
Copy link
Contributor

informatorius commented Feb 27, 2018

Currently we have:

  1. Simple mock:
var mock = new Mock<Interface>();
Interface instance = mock.Object;
  1. Mock with setable properties:
var mock = new Mock<Interface>();
mock.SetupAllProperties();
Interface instance = mock.Object;
----or----
Interface instance = Mock.Of<Interface>();
  1. Deep Mock
var mock = new Mock<Interface>{DefaultValue=DefaultValue.Mock};
Interface instance = mock.Object;
  1. Deep Mock with setable properties
var mock = new Mock<Interface>{DefaultValue = DefaultValue.Mock};
mock.SetupAllProperties();
Interface instance = mock.Object;

Would it be nice to have:

  1. Simple mock:
    var mock = new SimpleMock<Interface>();
  2. Mock with setable properties:
    var mock = new SimpleMockWithProperties<Interface>();
  3. Deep Mock
    var mock = new DeepMock<Interface>();
  4. Deep Mock with setable properties
    var mock = new DeepMockWithProperties<Interface>();

AND:

  1. Simple mock:
    Interface instance = Mock.Simple<Interface>();
  2. Mock with setable properties:
    Interface instance = Mock.SimpleWithProperties<Interface>();
  3. Deep Mock
    Interface instance = Mock.Deep<Interface>();
  4. Deep Mock with setable properties
    Interface instance = Mock.DeepWithProperties<Interface>();

(The Mock.DeepWithProperties<Interface>() would match nSubstitute.For<Interface>() )

@stakx
Copy link
Contributor

stakx commented Feb 27, 2018

@informatorius - Due to the Moq 4 API freeze requested by the project owner in the Moq Gitter chat, I think it'd be better to have this discussion over at moq/moq (the repo where Moq 5 is being developed) if it turns out that you actually want to change the API.

A couple of points:

  • I seem to remember that SetupAllProperties might become Moq 5's default behavior (but I might remember this incorrectly), so the distinction between what you call "simple mock" and "[simple] mock with set[t]able properties" would disappear.

  • Concerning the 3rd and 4th variant above, the distinction between a "simple" and "deep" mock appears to be derived directly from DefaultValue.Empty vs. DefaultValue.Mock. I don't think it makes sense to invent new terminology / names just for the sake of it. Am I missing something here?

@informatorius
Copy link
Contributor Author

My point is to get more easy syntax for create a mock.

e.g. for a class to test which takes 2 constructor parameters
MyClass(Interface1, Interface2){}

When I want deep mocks then I need to call it with
new MyClass(new Mock<Interface1>{DefaultValue=DefaultValue.Mock}.Object, new Mock<Interface2>{DefaultValue=DefaultValue.Mock}.Object)
I wish to call it just with
new MyClass(Mock.Deep<Interface1>(), Mock.Deep<Interface2>())

If this would be more easy or usefull also for others then it could make sense to create such API maybe for moq 5. What do you think about it?

@stakx
Copy link
Contributor

stakx commented Feb 28, 2018

The question should rather be what others think about it.

I personally don't see a big win. I'd prefer a smaller, tighter API. Having several ways to achieve the same thing can easily get confusing for those who are learning the API.

Btw. have you looked into MockRepository? For example:

var deep = new MockRepository { DefaultValue = DefaultValue.Mock };
new MyClass(deep.Create<Interface1>(), deep.Create<Interface2>());

@informatorius
Copy link
Contributor Author

Did not know it. MockRepository could be helpful too.

@stakx
Copy link
Contributor

stakx commented Mar 9, 2018

@informatorius, can we close this or is there something that you feel hasn't been addressed yet?

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

No branches or pull requests

2 participants