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

feat: Change return type on WithSelfMock #282

Merged
merged 2 commits into from
Feb 8, 2024

Conversation

BenjaminMichaelis
Copy link
Contributor

@BenjaminMichaelis BenjaminMichaelis commented Feb 2, 2024

This is a proposal as well as showing it.

This is all based on my assumption of how AutoMocker works :)
I think returning the mocked service here makes more sense than the object of the implementation.

  1. Having the actual instance only helps if I am about to go use the concrete type after (saving me one line of code at most).
    ex:
var implementedObject = Mocker.WithSelfMock<IStorage, RealStorage>();
foo.TryRenameAsync(implementedObject, newName))

But could be achieved by doing this in the new format:

var serviceMock = Mocker.WithSelfMock<IStorage, RealStorage>();
foo.TryRenameAsync(serviceMock.Object, newName))
  1. With the mock service, I can then so do a setup that I couldn't do immediately with the implemented object.
    before:
Mocker.WithSelfMock<IStorage, RealStorage>();
Mock<IStorage> mock = Mocker.GetMock<IStorage>();
mock.Setup(mock => mock.DoThing(name)).ReturnsAsync("Did Something");
foo.TryRenameAsync(mock.Object, newName))

can now be:

Mock<IStorage> mock = Mocker.WithSelfMock<IStorage, RealStorage>();
mock.Setup(mock => mock.DoThing(name)).ReturnsAsync("Did Something");
foo.TryRenameAsync(mock.Object, newName))
  1. The user doesn't loose out from the serviceMock being able to represent the concrete class, and can still call it if call base is passed in.
Mock<IStorage> mock = Mocker.WithSelfMock<IStorage, RealStorage>(callBase: true);
mock.Setup(mock => mock.DoThing(name)).ReturnsAsync("Did Something");
foo.TryRenameAsync(mock.Object, newName))

Moq.AutoMock/AutoMocker.cs Outdated Show resolved Hide resolved
@Keboo Keboo enabled auto-merge (squash) February 8, 2024 04:28
@Keboo Keboo force-pushed the BMichaelis/WithSelfMockReturn branch from 84801ef to ea2f3f7 Compare February 8, 2024 04:28
@Keboo Keboo merged commit 9b1cf1b into moq:master Feb 8, 2024
3 checks passed
@BenjaminMichaelis BenjaminMichaelis deleted the BMichaelis/WithSelfMockReturn branch February 8, 2024 04:49
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

Successfully merging this pull request may close these issues.

2 participants