2

Trying to get this simple test to work:

public class MyClass
{
    public string Text { get; set; }
    public List<string> Comments { get; set; }

}
[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
        var fixture = new Fixture();
        fixture.Customize<string>(c => c.FromSeed(s => s)); //Just return propertyname, no GUID appended
        var test = fixture.Create<MyClass>();

    }
}

but I keep getting the error

The decorated ISpecimenBuilder could not create a specimen based on the request: System.String. This can happen if the request represents an interface or abstract class; if this is the case, register an ISpecimenBuilder that can create specimens based on the request. If this happens in a strongly typed Build<T> expression, try supplying a factory using one of the IFactoryComposer<T> methods.`

If I remove the Customize line, is seems to work...

Not quite sure what I need to do to make it work

0

2 Answers 2

2

You can create string instances without appending a GUID by customizing the Fixture instance as shown below:

public void GuidsAreNotAppendedOnStringValues()
{
    var fixture = new Fixture();
    var expected = string.Empty;
    fixture.Customizations.Add(
        new StringGenerator(() => expected));

    var actual = fixture.Create<MyClass>();

    Assert.Equal(expected, actual.Comments.Aggregate((x, y) => x + y));
}

That way also the Text property is Text, instead of Texte85e2f6f-c1a3-47c7-baf2-4756c498f523.

Sign up to request clarification or add additional context in comments.

1 Comment

But then it seems like all the entries in my comment list are empty... would like them to be filled with something too...
0

Ended up, adding my own ISpecimenBuilder

public class TextBuilder : ISpecimenBuilder
{
    public object Create(object request, ISpecimenContext context)
    {
        var pi = request as PropertyInfo;
        if (pi == null)
            return new NoSpecimen(request);
        if (pi.PropertyType == typeof(string))
            return pi.Name;
        if (pi.PropertyType == typeof(IList<string>) || pi.PropertyType == typeof(List<string>))
        {
            var tmps = (List<string>)context.Resolve(typeof(List<string>));
            for (var n = 0; n != tmps.Count; ++n)
                tmps[n] = pi.Name + n.ToString(CultureInfo.InvariantCulture);
            return tmps;
        }
        return new NoSpecimen(request);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.