gpt4 book ai didi

c# - 对于 BDD/TDD 这种风格的改进有什么建议吗?

转载 作者:行者123 更新时间:2023-11-30 16:33:49 26 4
gpt4 key购买 nike

我正在尝试使用我们的单元测试规范进行设置

在场景 Y 中发生行为 X 时 SUT 的规范 鉴于这件事 还有这件事 当我做 X... 那么它应该做... 它也应该做...

我将 GivenThat 的每个步骤都包装在 Actions 中......是否有任何反馈,无论与 Actions 分开是好是坏,还是让 GivenThat 更清晰的更好方法?

        /// <summary>
/// Given a product is setup for injection
/// And Product Image Factory Is Stubbed();
/// And Product Size Is Stubbed();
/// And Drawing Scale Is Stubbed();
/// And Product Type Is Stubbed();
/// </summary>
protected override void GivenThat()
{
base.GivenThat();

Action givenThatAProductIsSetupforInjection = () =>
{
var randomGenerator = new RandomGenerator();

this.Position = randomGenerator.Generate<Point>();

this.Product = new Diffuser
{
Size =
new RectangularProductSize(
2.Inches()),
Position = this.Position,
ProductType =
Dep<IProductType>()
};
};

Action andProductImageFactoryIsStubbed = () => Dep<IProductBitmapImageFactory>().Stub(f => f.GetInstance(Dep<IProductType>())).Return(ExpectedBitmapImage);

Action andProductSizeIsStubbed = () =>
{
Stub<IDisplacementProduct, IProductSize>(p => p.Size);

var productBounds = new ProductBounds(Width.Feet(), Height.Feet());

Dep<IProductSize>().Stub(s => s.Bounds).Return(productBounds);
};

Action andDrawingScaleIsStubbed = () => Dep<IDrawingScale>().Stub(s => s.PixelsPerFoot).Return(PixelsPerFoot);

Action andProductTypeIsStubbed = () => Stub<IDisplacementProduct, IProductType>(p => p.ProductType);

givenThatAProductIsSetupforInjection();
andProductImageFactoryIsStubbed();
andProductSizeIsStubbed();
andDrawingScaleIsStubbed();
andProductTypeIsStubbed();
}

最佳答案

将它们放在不同的方法中,以便您可以将它们组合在其他给定的方法中。此外,使用下划线代替空格(而不是驼峰式)。此外,创建一个方法 Given_that 接受 Action 委托(delegate)的参数。

protected void Given_that(params Action[] preconditions)
{
foreach (var action in preconditions)
{
action();
}
}

...

protected void a_product_is_set_up_for_injection()
{
...
}

protected void product_image_factory_is_stubbed()
{
...
}

etc...

...

Given_that(a_product_is_set_up_for_injection,
product_image_factory_is_stubbed,
product_size_is_stubbed,
drawing_scale_is_stubbed,
product_type_is_stubbed);

话虽这么说,我认为你的先决条件的命名不是 BDD。它们本质上是非常技术性的,并不表示业务需求。如果您要告诉非程序员您正在测试什么,您可能不会说“该产品已 stub 用于注入(inject)”。你更有可能说

Given a displacement product
that is a two inch rectangular diffuser
that has a random position
that has a bitmap
that has a size bounded by feet
that has the expected pixels per foot

现在您可以编写您的“给定”方法,几乎​​没有重复:

protected [the type of your test class] Given(params Action given)
{
given();
return this;
}

protected void That(params Action[] preconditions)
{
foreach (var precondition in preconditions)
{
precondition();
}
}

Given(a_displacement_product)
.That(is_a_two_inch_rectangular_diffuser,
has_a_random_position,
has_a_bitmap,
has_a_size_bounded_by_feet,
has_the_expected_pixels_per_foot);

关于c# - 对于 BDD/TDD 这种风格的改进有什么建议吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2883939/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com