Skip to content
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
71 changes: 44 additions & 27 deletions Moq.AutoMock.Tests/DescribeCreatingSelfMocks.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,44 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq.AutoMock.Tests.Util;

namespace Moq.AutoMock.Tests
{
[TestClass]
public class DescribeCreatingSelfMocks
{
[TestMethod]
public void Self_mocks_are_useful_for_testing_most_of_class()
{
var mocker = new AutoMocker();
var selfMock = mocker.CreateSelfMock<InsecureAboutSelf>();
selfMock.TellJoke();
Assert.IsFalse(selfMock.SelfDepricated);
}

[TestMethod]
public void It_can_self_mock_objects_with_constructor_arguments()
{
var mocker = new AutoMocker();
var selfMock = mocker.CreateSelfMock<WithService>();
Assert.IsNotNull(selfMock.Service);
Assert.IsNotNull(Mock.Get(selfMock.Service));
}
}
}
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq.AutoMock.Tests.Util;

namespace Moq.AutoMock.Tests
{
[TestClass]
public class DescribeCreatingSelfMocks
{
[TestMethod]
public void Self_mocks_are_useful_for_testing_most_of_class()
{
var mocker = new AutoMocker();
var selfMock = mocker.CreateSelfMock<InsecureAboutSelf>();
selfMock.TellJoke();
Assert.IsFalse(selfMock.SelfDepricated);
}

[TestMethod]
public void It_can_self_mock_objects_with_constructor_arguments()
{
var mocker = new AutoMocker();
var selfMock = mocker.CreateSelfMock<WithService>();
Assert.IsNotNull(selfMock.Service);
Assert.IsNotNull(Mock.Get(selfMock.Service));
}

[TestMethod]
[Description("Issue 130")]
public void It_reuses_dependencies_when_creating_self_mock()
{
var mocker = new AutoMocker();
var service = mocker.CreateSelfMock<AbstractService>();
Assert.IsTrue(ReferenceEquals(service.Dependency, mocker.GetMock<IDependency>().Object));
}

public abstract class AbstractService
{
public IDependency Dependency { get; }
public AbstractService(IDependency dependency) => Dependency = dependency;
}

public interface IDependency { }
}
}
4 changes: 3 additions & 1 deletion Moq.AutoMock/AutoMocker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,14 @@ public T CreateSelfMock<T>() where T : class?
public T CreateSelfMock<T>(bool enablePrivate) where T : class?
{
var context = new ObjectGraphContext(enablePrivate);
if (!TryGetConstructorInvocation(typeof(T), context, out _, out IInstance[]? arguments))
if (!TryGetConstructorInvocation(typeof(T), context, out ConstructorInfo? ctor, out IInstance[]? arguments))
{
throw new ArgumentException(
$"Did not find a best constructor for `{typeof(T)}`. If your type has a non-public constructor, set the 'enablePrivate' parameter to true for this {nameof(AutoMocker)} method.");
}

CacheInstances(arguments.Zip(ctor.GetParameters(), (i, p) => (p.ParameterType, i)));

var mock = new Mock<T>(MockBehavior, arguments.Select(x => x.Value).ToArray())
{
DefaultValue = DefaultValue,
Expand Down