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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions Moq.AutoMock.Tests/DescribeWithSelfMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public void It_can_register_a_custom_default_value_provider_for_a_self_mock_with

var mock = mocker.WithSelfMock<IService2, Service2>(defaultValue: DefaultValue.Custom, defaultValueProvider: provider);

Assert.AreEqual(provider, Mock.Get(mock).DefaultValueProvider);
Assert.AreEqual(provider, mock.DefaultValueProvider);
}

[TestMethod]
Expand All @@ -24,7 +24,7 @@ public void It_uses_default_value_provider_for_a_self_mock_with_interface()

var mock = mocker.WithSelfMock<IService2, Service2>();

Assert.AreEqual(provider, Mock.Get(mock).DefaultValueProvider);
Assert.AreEqual(provider, mock.DefaultValueProvider);
}

[TestMethod]
Expand Down Expand Up @@ -98,5 +98,14 @@ public void It_uses_default_value_provider_for_a_self_mock_with_class_type()

Assert.AreEqual(provider, Mock.Get(mock).DefaultValueProvider);
}

[TestMethod]
public void WithSelfMock_returns_mock_of_service()
{
var mocker = new AutoMocker();
var mockService = mocker.WithSelfMock<IService2, Service2>();

Assert.IsInstanceOfType<Mock<IService2>>(mockService);
}
}

11 changes: 7 additions & 4 deletions Moq.AutoMock/AutoMocker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,8 @@ public T CreateSelfMock<T>(
/// <param name="defaultValue">Sets the DefaultValue property on the created Mock.</param>
/// <param name="defaultValueProvider">The instance that will be used to produce default return values for unexpected invocations.</param>
/// <param name="callBase">Sets the CallBase property on the created Mock.</param>
/// <returns>An instance with virtual and abstract members mocked</returns>
public TImplementation WithSelfMock<TService, TImplementation>(
/// <returns>A mock of the service</returns>
public Mock<TService> WithSelfMock<TService, TImplementation>(
bool enablePrivate = false,
MockBehavior? mockBehavior = null,
DefaultValue? defaultValue = null,
Expand All @@ -320,12 +320,15 @@ public TImplementation WithSelfMock<TService, TImplementation>(
defaultValue ?? DefaultValue,
defaultValueProvider ?? DefaultValueProvider,
callBase ?? CallBase);

Mock<TService> serviceMock = selfMock.As<TService>();

WithTypeMap(typeMap =>
{
typeMap[typeof(TImplementation)] = new MockInstance(selfMock);
typeMap[typeof(TService)] = new MockInstance(selfMock.As<TService>());
typeMap[typeof(TService)] = new MockInstance(serviceMock);
});
return selfMock.Object;
return serviceMock;
}

/// <summary>
Expand Down
Loading