Skip to content
Merged
Changes from 2 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
52 changes: 52 additions & 0 deletions Moq.AutoMock.Tests/DescribeCreateInstance.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq.AutoMock.Resolvers;
using Moq.AutoMock.Tests.Util;

namespace Moq.AutoMock.Tests;
Expand Down Expand Up @@ -151,4 +152,55 @@ public void It_throws_when_creating_object_with_recursive_dependency()
ArgumentException e = Assert.ThrowsException<ArgumentException>(mocker.CreateInstance<WithRecursiveDependency>);
Assert.IsTrue(e.Message.StartsWith($"Did not find a best constructor for `{typeof(WithRecursiveDependency)}`"));
}


[TestMethod]
[Description("Issue 123")]
public void It_can_use_fixed_value_to_supply_string_parameter()
{
AutoMocker mocker = new();
mocker.Use("Test string");
HasStringParameter sut = mocker.CreateInstance<HasStringParameter>();

Assert.AreEqual("Test string", sut.String);
}

[TestMethod]
[Description("Issue 123")]
public void It_can_use_custom_resolver_to_supply_string_parameter()
{
AutoMocker mocker = new();
mocker.Resolvers.Add(new CustomStringResolver("Test string"));
HasStringParameter sut = mocker.CreateInstance<HasStringParameter>();

Assert.AreEqual("Test string", sut.String);
}

private class CustomStringResolver : IMockResolver
{
public CustomStringResolver(string stringValue)
{
StringValue = stringValue;
}

public string StringValue { get; }

public void Resolve(MockResolutionContext context)
{
if (context.RequestType == typeof(string))
{
context.Value = StringValue;
}
}
}

public class HasStringParameter
{
public HasStringParameter(string @string)
{
String = @string;
}

public string String { get; }
}
}