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.

Building a customized instance fails on circular references

See original GitHub issue
    [TestFixture]
    public class TestAutoFixture4
    {
        [Test]
        [AutoMoqData]
        public void CreatingAnDummyObjectShouldNotThrow(IFixture fixture)
        {
            fixture.Invoking(x => x.Create<DummyObject>()).ShouldNotThrow();
        }

        [Test]
        [AutoMoqData]
        public void BuildingAnDummyObjectShouldNotThrow(IFixture fixture)
        {
            fixture.Invoking(x => x.Build<DummyObject>().Create()).ShouldNotThrow();
        }
    }

    public class DummyObject
    {
        public Guid Id { get; set; }
        public DummyObject CircularRef { get; set; }
    }

    public class AutoMoqDataAttribute : AutoDataAttribute
    {
        public AutoMoqDataAttribute()
            : base(new Fixture()
                 .Customize(
                    new DummyCustomization()
                ))
        { }
    }

    public class DummyCustomization : ICustomization
    {
        public void Customize(IFixture fixture)
        {
            fixture.Customize<DummyObject>(c =>
                c.Without(x => x.CircularRef)
            );
        }
    }

Just updated AutoFixture from 3.50.6 to 4.0.0.

The above code works perfectly on 3.50.6, but only the first test (using fixture.Create) passes using 4.0.0 (.NET 4.6.2, NUnit3.7.1, FluentAssertions 4.19.4).

The test using fixture.Build fails, even if the injected customization specifies not to resolve the circular reference.

Did I miss something in the changelog or is it a bug ?

Thanks in advance 😃

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
zvirjacommented, Jan 15, 2018

Do you think the Omitter approach can solve the problem ? I didn’t know this class and will definitely try it anyway.

If you rewrite your customization as following, all the tests will start to pass:

public class DummyCustomization : ICustomization
{
    public void Customize(IFixture fixture)
    {
        // Don't populate the DummyObject.CircularRef property.
        fixture.Customizations.Add(
            new Omitter(
                new EqualRequestSpecification(
                    typeof(DummyObject).GetProperty(nameof(DummyObject.CircularRef)))));
    }
}

Therefore, yep, it should fix your issue 😉 Also this approach doesn’t look too cumbersome, so should be acceptable.

Please let me know if you have further questions on your question.

0reactions
zvirjacommented, Jan 15, 2018

@Dev-I-Ant Awesome, thanks for the feedback! 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Resolve build errors due to circular dependency amongst ...
The reason this fix is bad is because the next person to #include "A.h" will have to declare B before they can use...
Read more >
code quality - What's wrong with circular references?
Circular object references can crash naïve recursive algorithms (such as serializers, visitors and pretty-printers) with stack overflows.
Read more >
How To Find and Remove Circular References in Excel
Create a new circular reference after removing previous instances. Open a workbook with an unresolved circular reference. The error message ...
Read more >
Multiple Component Instances - Circular Reference Error
I have created a Component that is basically a Gallery of Buttons - it has input Parameters of "Items" (Single column Table of...
Read more >
Remove or allow a circular reference
You remove all circular references in all open workbooks, and then create a new circular reference. You close all workbooks, create a new...
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