AutoFixture 2 With() isn't working as it did in AutoFixture 1?

怎甘沉沦 提交于 2019-12-10 16:47:11

问题


I'm porting my tests to AutoFixture 2.0, and I've run in to some strange behavior that I can neither explain nor fix. This simple test is failing for me:

var autoFixtures = new Fixture();
var file = autoFixtures.Build<File>()
                       .With(f => f.Name, "bleh.txt")
                       .CreateAnonymous();

Assert.That(file.Name, Is.EqualTo("bleh.txt"));  // Fail?!

The test succeeds if I change Name to another property of File, which leads me to think that I have some sort of customization present for Name that wasn't working when I was using AutoFixture 1.0. I've scoured my code, though, and I can't find anything like that.

Enabling tracing doesn't seem to help much.

autoFixtures.Behaviors.Add(new TracingBehavior());

displays, among other stuff:

Requested: System.String Name
    Requested: Ploeh.AutoFixture.Kernel.SeededRequest
    Created: Ploeh.AutoFixture.Kernel.NoSpecimen
  Requested: Ploeh.AutoFixture.Kernel.SeededRequest
    Requested: System.String
    Created: Ploeh.AutoFixture.Kernel.NoSpecimen
    Requested: System.String
    Created: 8a022fda-fa4e-49b7-b0c2-285fef765386
  Created: Name8a022fda-fa4e-49b7-b0c2-285fef765386
Created: Name8a022fda-fa4e-49b7-b0c2-285fef765386

FWIW, Name is declared as a virtual property of File's base class, and then overridden in File as such:

public abstract class Item
{
    public virtual string Name { get; set; }
    ...
}

public class File : Item
{
    public override string Name { get; set; }
    ...
}

If anyone has any ideas on something I might try, or somewhere I might have inadvertently customized the behavior of the Name property, I would be most appreciative!


回答1:


You just found a bug in AutoFixture 2.0. I have now resolved it and pushed the changes to the repository (changeset 3efe812aecd1), so if you download the latest source and compile it, it should work.

In case you are interested, it was related to a virtual property being overridden by a child class - apparently not something I do very often.

Sorry about the bug. Please let me know if the issue hasn't been resolved, or if you have other questions.



来源:https://stackoverflow.com/questions/4650424/autofixture-2-with-isnt-working-as-it-did-in-autofixture-1

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!