Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set the properties of Fixture object #63

Open
sergioprates opened this issue Jul 28, 2023 · 1 comment
Open

How to set the properties of Fixture object #63

sergioprates opened this issue Jul 28, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@sergioprates
Copy link

Describe the feature

Hi, nice lib!

We have a away to set the properties of Fixture object? Like:

var fixture = new Fixture { RepeatCount = 1, };

@sergioprates sergioprates added the enhancement New feature or request label Jul 28, 2023
@victorsebrito
Copy link
Owner

@sergioprates Hi! Thanks!

That should be possible after #10. There's a constructor in BuilderBase that receives a fixture:

protected BuilderBase(Fixture fixture)
: this(CreateComposer(fixture))
{
}

You'd have to create your own Builder class though. This unit test might give you an idea of how to use it:

public void ShouldRespectFixtureCustomizations()
{
var foo = new FooBuilderCustomFixture()
.Create();
using (new AssertionScope())
{
foo.Bar.Should().NotBeNull("the builder should generate all public properties");
foo.Bar?.Name.Should().Be(
FooBuilderCustomFixture.NAME,
"the builder should respect any Fixture customizations");
}
}

internal class FooBuilderCustomFixture : BuilderBase<Foo, FooBuilderCustomFixture>
{
internal static readonly string NAME = Guid.NewGuid().ToString();
public FooBuilderCustomFixture()
: base(CreateFixture())
{
}
private static Fixture CreateFixture()
{
var fixture = new Fixture
{
OmitAutoProperties = false,
};
fixture.Customize<Bar>(bar => bar.With(x => x.Name, NAME));
return fixture;
}
}

I honestly don't remember why I didn't make it available in the concrete Builder class. I guess I just didn't needed it and was just playing around. I'll see what I can do!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants