Skip to content

Commit

Permalink
feat: Change return type on WithSelfMock (#282)
Browse files Browse the repository at this point in the history
* Change return type

* use local variable instead of re-calling `.As()`
  • Loading branch information
BenjaminMichaelis authored Feb 8, 2024
1 parent bad9d83 commit 9b1cf1b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
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

0 comments on commit 9b1cf1b

Please sign in to comment.