From 40df6dcee300a2bba6870f5b4205397e750f7236 Mon Sep 17 00:00:00 2001 From: Kevin B Date: Sun, 20 Feb 2022 22:19:04 -0800 Subject: [PATCH] Adding tests showing how to provide custom string resolution (#138) --- Moq.AutoMock.Tests/DescribeCreateInstance.cs | 52 ++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/Moq.AutoMock.Tests/DescribeCreateInstance.cs b/Moq.AutoMock.Tests/DescribeCreateInstance.cs index 6e62d09f..2c85de9c 100644 --- a/Moq.AutoMock.Tests/DescribeCreateInstance.cs +++ b/Moq.AutoMock.Tests/DescribeCreateInstance.cs @@ -1,5 +1,6 @@ using System; using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq.AutoMock.Resolvers; using Moq.AutoMock.Tests.Util; namespace Moq.AutoMock.Tests; @@ -151,4 +152,55 @@ public void It_throws_when_creating_object_with_recursive_dependency() ArgumentException e = Assert.ThrowsException(mocker.CreateInstance); 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(); + + 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(); + + 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; } + } }