BDD/TDD 以棘手的方式模拟数据

发布于 2024-08-19 15:21:18 字数 740 浏览 3 评论 0原文

因此,我和一位同事正在进行一场相当激烈的辩论。我们正在开始一个新项目,并尝试使用 BDD。我们都是新手,并不完全明白应该采用什么做法。我们已经编写了一些规范,现在正在实施代码。由于存在大量数据库交互,事情变得非常棘手。我们陷入了如何模拟数据的困境。我们正在讨论的方法需要我们模拟我们的方法而不是我们的数据。如果我用代码向您展示,这是最简单的...

public static void AssignLeadToDistributor(int leadId, int distributorId)
{
    Lead lead = GetById(leadId);
    lead.DistributorId = distributorId;
    Save(lead);
}

基本上,我们必须重写 GetById() 和 Save() 以返回模拟数据,以便我们进行测试。 这样做似乎更有意义:

public static void AssignLeadToDistributor(Lead lead, Distributor distributor)
{
   lead.DistributorId = distirbutor.Id;
}

然后我们可以模拟我们的对象。

显然第二种方法更容易测试。然而,争论的焦点是我们不想在前端代码上获取新的线索和分销商对象,​​因为只传递对象的 id 会更容易。减少前端的实际代码。

希望我解释得足够好。

你们觉得怎么样?哪种方式更有意义?

So a coworker and I are in a fairly heated debate. We are beginning a new project and we are attempting to use BDD. We are both first-timers and don't fully understand what practices should be used. We've written some specs and we are now implementing the code. Things are getting pretty tricky since there is a lot of database interaction. We are stuck on how we should mock our data. The method we were going about would require us to mock our methods instead of our data. It's easiest if I show you in code...

public static void AssignLeadToDistributor(int leadId, int distributorId)
{
    Lead lead = GetById(leadId);
    lead.DistributorId = distributorId;
    Save(lead);
}

Basically, we would have to override GetById() and Save() to return mock data for us to test this.
It seems to make more sense to do it like this:

public static void AssignLeadToDistributor(Lead lead, Distributor distributor)
{
   lead.DistributorId = distirbutor.Id;
}

Then we could just mock our objects.

Clearly the second method makes it easier to test. However, the argument is that we don't want to have to fetch a new lead and distributor object on our front end code, because it would be easier to just pass the ids of our objects. Cutting down on the actual code in our front end.

Hopefully I explained that well enough.

What do you guys think? Which way makes more sense?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

好菇凉咱不稀罕他 2024-08-26 15:21:18

我认为你遇到的最大问题是因为你使用了公共静态函数(这在面向对象语言中通常是一件坏事)。

我建议将此函数移至 Lead 对象,例如

public AssignDistributor(int distributorId) {
   this.DistributorId = distributorId;
   this.Save();
}

更易于测试,以及更多类似 OO 的代码 =)

I think the biggest problem you're having is because you're using public static functions (which is usually a bad thing in OO languages).

I'd suggest moving this function to the Lead object, something like

public AssignDistributor(int distributorId) {
   this.DistributorId = distributorId;
   this.Save();
}

Easier to test, and more OO-like code =)

握住你手 2024-08-26 15:21:18

我们在 BDD 规范(可执行故事)中所做的就是根本不模拟数据库,而是使用内存数据库(在我们的例子中是 SQLite)。

此外,我们在任何场景运行之前初始化容器。这是因为我们希望 BDD 规范尽可能模仿现实世界,同时仍然具有普通单元测试的速度。

通过以这种方式定义我们的 BDD 规范,我们发现对单元测试和集成测试的需求减少了,并且提高了生产力和可理解性(尽管非常主观,因为您无法真正衡量这些指标)。

What we do in our BDD specs (executable stories), is to not mock the DB at all, but instead use an in-memory DB (SQLite in our case).

Also, we initialize the container before any scenario runs. This is because we'd like our BDD specs to mimic the real world as far as possible, while still having the speed of ordinary unit tests.

By defining our BDD specs this way, we've found the need for unit tests and integration tests decrease, and gained both increased productivity and understandability (although very subjective since you can't really measure those metrics).

寂寞花火° 2024-08-26 15:21:18

我更喜欢第二种方法,因为您所说的原因:您可以轻松模拟参数以进行测试。您使用依赖注入框架吗?如果没有,那么我建议您使用依赖注入原理来编程您的方法,以获得更加模块化且易于测试的代码。

我同意塞缪尔的观点,您需要尽可能避免使用静态方法,否则您会发现测试驱动它们非常困难。

I like the second method better, for the reason you stated: you can mock the parameters easily for testing. Are you using a dependency injection framework? If not then you I'd recommend you program your methods using the Dependency Injection principle anyway for more modular, and easy to test code.

And I agree with Samuel that you need to avoid using static methods whenever possible, or you'll find it very difficult to test-drive them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文