Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to AutoFixture.CreateAnonymous<>() on class with internal ctor

I have done something like the following

public class Foo
{
    public Bar { get; set; }
}

public class Bar
{
    internal Bar(string id)
    {}
}

public static class Bars
{
     public static class TypeOne
     {
           public static readonly SimpleBar = new Bar("id-for-type1-simple-bar");
     }
}

now, I added an InternalsVisibleTo to my in my unit-test-assembly to be able to access the internal Bar ctor and then I did:

var fixture = new Fixture();
fixture.Register(() => new Bar(fixture.CreateAnonymous<string>()));
var foo = fixture.CreateAnonymous<Foo>();

Question:
Was that the "right" way to utilize AutoFixture or is there a better one?

like image 898
Nils Avatar asked Oct 27 '25 06:10

Nils


1 Answers

Couldn't you just do something like this instead?

fixture.Inject(Bars.TypeOne.SimpleBar);

This is all public. No need for InternalsVisibleTo, I think...

like image 149
Mark Seemann Avatar answered Oct 28 '25 21:10

Mark Seemann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!