【问题标题】:AutoFixture 2 With() isn't working as it did in AutoFixture 1?AutoFixture 2 With() 不像在 AutoFixture 1 中那样工作?
【发布时间】:2011-01-10 19:03:58
【问题描述】:

我正在将我的测试移植到AutoFixture 2.0,我遇到了一些我无法解释或修复的奇怪行为。这个简单的测试对我来说失败了:

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?!

如果我将Name 更改为File 的另一个属性,则测试成功,这让我认为我为Name 提供了某种自定义,当我使用AutoFixture 1.0 时它不起作用。不过,我已经搜索了我的代码,但找不到类似的东西。

启用跟踪似乎没有多大帮助。

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

显示,除其他外:

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 被声明为 File 的基类的虚拟属性,然后在 File 中被覆盖:

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

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

如果有人对我可能尝试的事情有任何想法,或者我可能无意中自定义了 Name 属性的行为,我将不胜感激!

【问题讨论】:

    标签: autofixture


    【解决方案1】:

    您刚刚在 AutoFixture 2.0 中发现了一个错误。我现在已经解决了它并将更改推送到存储库(更改集 3efe812aecd1),所以如果你 download the latest source 并编译它,它应该可以工作。

    如果您感兴趣,它与被子类覆盖的虚拟属性有关 - 显然不是我经常做的事情。

    抱歉这个错误。如果问题尚未解决,或者您还有其他问题,请告诉我。

    【讨论】:

    • 哎哟!至少,这让我现在花很多时间在这上面感觉好多了。很高兴一切都解决了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多