|
1 | 1 | using System; |
2 | 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 3 | +using Moq.AutoMock.Resolvers; |
3 | 4 | using Moq.AutoMock.Tests.Util; |
4 | 5 |
|
5 | 6 | namespace Moq.AutoMock.Tests; |
@@ -151,4 +152,55 @@ public void It_throws_when_creating_object_with_recursive_dependency() |
151 | 152 | ArgumentException e = Assert.ThrowsException<ArgumentException>(mocker.CreateInstance<WithRecursiveDependency>); |
152 | 153 | Assert.IsTrue(e.Message.StartsWith($"Did not find a best constructor for `{typeof(WithRecursiveDependency)}`")); |
153 | 154 | } |
| 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 | + } |
154 | 206 | } |
0 commit comments