nickdodd79 / AutoBogus

A C# library complementing the Bogus generator by adding auto creation and population capabilities.
MIT License
436 stars 50 forks source link

Object of type 'System.Collections.Generic.List`1[System.String]' cannot be converted to type 'System.Collections.Generic.IReadOnlySet`1[System.String]'. #106

Open jan-nemo opened 1 year ago

jan-nemo commented 1 year ago

Hello,

I'm encountering an issue with AutoBogus v2.13.1. The problem arises when attempting to generate an entity that includes an IReadOnlySet property. Here's the relevant code snippet:

var mockSettings = new AutoFaker<MockSettings>()
    .UseSeed(100)
    .Generate();

sealed record MockSettings(IReadOnlySet<string> MockedOperations);

Unfortunately, this leads to the following error:

at System.RuntimeType.CheckValue(Object& value, ParameterCopyBackAction& copyBack, Binder binder, CultureInfo culture, BindingFlags invokeAttr)
   at System.Reflection.MethodBase.CheckArguments(Span`1 copyOfParameters, IntPtr* byrefParameters, Span`1 shouldCopyBack, ReadOnlySpan`1 parameters, RuntimeType[] sigTypes, Binder binder, CultureInfo culture, BindingFlags invokeAttr)
   at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.ConstructorInfo.Invoke(Object[] parameters)
   at AutoBogus.AutoBinder.CreateInstance[TType](AutoGenerateContext context)
   at AutoBogus.AutoFaker`1.<>c__DisplayClass29_0.<PrepareCreate>b__0(Faker faker)
   at Bogus.Faker`1.Generate(String ruleSets)
   at AutoBogus.AutoFaker`1.Generate(String ruleSets)
   at Program.<Main>$(String[] args) in D:\Test\Program.cs:line 12

I've also attempted to set a specific rule for this property as shown below, but the error persists:

var mockSettings = new AutoFaker<MockSettings>()
    .UseSeed(100)
    .RuleFor(f => f.MockedOperations, new HashSet<string>() { "MockedOperation1", "MockedOperation2", "MockedOperation3" })
    .Generate();

Could you please assist me with this? Thanks in advance!

PS: it seems like there was another similar issue but with ISet.

soenneker commented 8 months ago

Due to the performance issues and missing types etc I've decided to build an updated version of AutoBogus: soenneker.utils.autobogus

I'll add support for IReadOnlySet in my lib!

soenneker commented 7 months ago

@jan-nemo IReadOnlySet is now implemented in soenneker.utils.autobogus