Hey is it possible to generate a random instance b...
# kotest
a
Hey is it possible to generate a random instance based on a class using Arb? something like Arb.next<MyType>()?
e
Yes. Arb.bind or Arb.data (Alias for bind)
Copy code
Arb.bind(
  <http://Arb.int|Arb.int>(),
  Arb.string(codepoints = Codepoint.alphanumeric()),
  ::Student
)
Another option:
Copy code
Arb.data<Student>(providedArbs = mapOf(String::class to Codepoint.alphanumeric()))
a bit tricky when using String for instance, since the provided arbs will work for all values of that type
a
Ya true
d
You can also use DataFaker to have some more realistic names and combine that with your custom generator 🙂
a
I just wanted to override a specific field without the need to define every arb for every field with
arbitrary
block
d
I guess you could do it like this:
Copy code
val studentArb = arbitrary {
            val student = Arb.data<Student>().bind()
            student.copy(name = Arb.string(codepoints = Codepoint.alphanumeric()).bind())
        }
But I agree it would be good to have more control over the generation for specific fields
The providerArbs in Arb.data works on types, not field names, so some sort of generator with field names could be useful
In this case the example is small enough that I wouldn't mind a simple arbitrary and just map the fields explicitly
e
Reflective binding with overriding per field should be doable I think. Worth investigating
a
I totally agree for instance im having issues because the default Arb for OffsetDateTime uses too much memory resulting in out of memory exception in my unit tests 😛
d
Probably due to the zoneOffsets 🙂
e
Created a POC.. definitely seems doable 🙂
Shit.. I was hoping the cast would filter out properties for irrelevant classes, but seems to pass due to type-erasure 😞 nvm, solved. was possible to reflectively check the type of the property to make sure it matched the class being constructed
133 Views