question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using a rule for a specific property when building a class

See original GitHub issue

Hi.

Is it possible to make AutoFixture randomly pick one element in a predefined list (or an array) for a specific property? Something like:

public class SomeClass
{
    public char SomeProperty { get; set; }
    ...
}

fixture.Build<SomeClass>()
    .With(cls => cls.SomeProperty, [SomeKindOfARandomlyPicker](new char[]{ 'S', 'N' }))
    .Create();

Thanks.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
crenancommented, Mar 30, 2020

.With(p => p.Property1, (Generator<char> generator) => generator.Where(c => validChars.Contains©).First())

@Kralizek Thank you. It’s ugly but works 😄 .

As for the hint for @zvirja, the first thing I tried was using the ElementsBuilder<char>('N','S'), but I was getting the exception you mentioned, so I went to check the code and saw that it’s not possible to use an ISpecimenBuilder.

It would be a good thing the possibility to use an ISpecimenBuilder for a specific property, like

.With(p => p.MyProperty, new SpecimenBuilder(....))

so AF could use it to generate values in the post-processing.

1reaction
Kralizekcommented, Mar 30, 2020

@crenan Sorry, you are right. I could only find a workaround to get to work.

char[] validChars = new char[]{'Y', 'N'};

Fixture fixture = new Fixture();

var test = fixture.Build<TestClass>()
			.With(p => p.Property1, (Generator<char> generator) => generator.Where(c => validChars.Contains(c)).First())
			.Create();

It’s not clean but it works.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rule Class (Microsoft.Build.Framework.XamlTypes)
This is used to specify whether multiple files need to be batched on one command line invocation. The prefix to use for all...
Read more >
How To Create Classes With CSS
In this tutorial, you will create a CSS class selector, which will allow you to apply CSS rules only to HTML elements that...
Read more >
Representing Classes As Property Values on the Semantic ...
Approach 1: Use classes directly as property values · Approach 2: Create special instances of the class to be used as property values...
Read more >
Defining the pages and classes of a rule
To set the class of the primary page, add a row that has no value for the page name and the desired class...
Read more >
Specificity - CSS: Cascading Style Sheets - MDN Web Docs
Specificity is an algorithm that calculates the weight that is applied to a given CSS declaration. The weight is determined by the number...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found