Skip to content

Commit

Permalink
Adding tests showing how to provide custom string resolution (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
Keboo committed Feb 21, 2022
1 parent 2d3b6f9 commit 40df6dc
Showing 1 changed file with 52 additions and 0 deletions.
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; }
}
}

0 comments on commit 40df6dc

Please sign in to comment.