Skip to content

Commit 40df6dc

Browse files
authored
Adding tests showing how to provide custom string resolution (#138)
1 parent 2d3b6f9 commit 40df6dc

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

Moq.AutoMock.Tests/DescribeCreateInstance.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using Moq.AutoMock.Resolvers;
34
using Moq.AutoMock.Tests.Util;
45

56
namespace Moq.AutoMock.Tests;
@@ -151,4 +152,55 @@ public void It_throws_when_creating_object_with_recursive_dependency()
151152
ArgumentException e = Assert.ThrowsException<ArgumentException>(mocker.CreateInstance<WithRecursiveDependency>);
152153
Assert.IsTrue(e.Message.StartsWith($"Did not find a best constructor for `{typeof(WithRecursiveDependency)}`"));
153154
}
155+
156+
157+
[TestMethod]
158+
[Description("Issue 123")]
159+
public void It_can_use_fixed_value_to_supply_string_parameter()
160+
{
161+
AutoMocker mocker = new();
162+
mocker.Use("Test string");
163+
HasStringParameter sut = mocker.CreateInstance<HasStringParameter>();
164+
165+
Assert.AreEqual("Test string", sut.String);
166+
}
167+
168+
[TestMethod]
169+
[Description("Issue 123")]
170+
public void It_can_use_custom_resolver_to_supply_string_parameter()
171+
{
172+
AutoMocker mocker = new();
173+
mocker.Resolvers.Add(new CustomStringResolver("Test string"));
174+
HasStringParameter sut = mocker.CreateInstance<HasStringParameter>();
175+
176+
Assert.AreEqual("Test string", sut.String);
177+
}
178+
179+
private class CustomStringResolver : IMockResolver
180+
{
181+
public CustomStringResolver(string stringValue)
182+
{
183+
StringValue = stringValue;
184+
}
185+
186+
public string StringValue { get; }
187+
188+
public void Resolve(MockResolutionContext context)
189+
{
190+
if (context.RequestType == typeof(string))
191+
{
192+
context.Value = StringValue;
193+
}
194+
}
195+
}
196+
197+
public class HasStringParameter
198+
{
199+
public HasStringParameter(string @string)
200+
{
201+
String = @string;
202+
}
203+
204+
public string String { get; }
205+
}
154206
}

0 commit comments

Comments
 (0)