英文:
How to apply TDD if one does not know how the code is supposed to work or be implemented?
问题
I understand your request to translate the text you provided. Here's the translated text:
首先,请原谅我如果我的问题有些奇怪或者解决方案很明显。我已经在互联网上搜索了几个月,寻找一个合适的回答,但始终没有找到我想要的答案。
我已经练习了一年以上的测试驱动开发,始终以测试行为而不是实现为目标。
想象一下以下情景作为一个简单的例子,使用标准的ReactJS应用程序:
-
我想创建一个"照片标记器",用户点击一张图片,如果点击与特定人物或对象的坐标匹配,则应用程序返回true。由于我想应用TDD,我不应该编写伪代码,而是让我的测试来设计开发。
-
我知道我可能需要玩弄
getBoundingClientRect
,但我需要实际练习来理解这个方法的工作方式,因为我以前从未使用过它。 -
现在我处于一个情况,我不被允许在编写失败的测试之前编写任何代码,但我不知道测试应该包含什么,或者如何验证我的目标。
在浏览互联网时,我没有看到许多关于这个问题的解决方案,或者也许我没有理解我所阅读的与我的问题有什么关系。
有人建议先写代码,然后再写测试。然后我会先提交测试,然后手动提交代码,这样测试看起来是首先创建的。这对我来说似乎非常不正当和不诚实。
其他人建议只在您确定测试方法的情况下才使用TDD,这似乎违背了TDD的初衷。
我自己想出了一些解决方案,但我不确定它们是否适用于工作环境:
-
创建一个名为"playground"的文件,与项目分开以进行实验。
-
创建一个名为"Experimenting with feature"的分支,随意编写代码而不测试,然后在代码工作时删除它,然后在主分支中重新测试。这是否奇怪?
-
对于我不知道如何编码的事情,完全不考虑TDD,先写代码,然后再写测试。
-
写一个空的测试,使其在编译时失败,然后编写代码,随后再制定测试内容。
我会很感激对我的想法的任何反馈,尤其是您对此问题的看法。我一直陷入一个循环中,不敢尝试在没有首先编写测试的情况下对代码进行实验,又无法编写测试。
英文:
First of all please do forgive me if my question is odd or its solution obvious. I have searched the internet for months for an appropriate reply and never found quite what I was looking for.
I have been practicing test driven development for over a year, always aiming to test behavior & not implementation.
Imagine the following scenario as a simple example, with a standard ReactJS app:
I want to create a 'photo-tagger', where a user clicks on an image & the app returns true if the click matches the coordinates of a specific person or object.
Since I want to apply TDD, I am not supposed to write pseudocode and instead let my tests design the development.
2.
I know that I'll probably have to play around with getBoundingClientRect
, but I need actual practice to understand how this method works, because I've never used it before.
I am now in a situation where I am not allowed to write any code before writing a failing test first, but I have no idea what the test should consist of or how to validate my goal.
Browsing the internet, I've not seen many solutions to this, or perhaps I didn't understand how what I was reading related to my issue.
One post suggested to write the code first, then the test. I'd then commit the test first then the code manually, so the test appears to have been created first. This appears very hacky & dishonest to me.
Others suggested using TDD only in scenarios where you were sure of the testing approach, which seems to defeat the point of TDD.
I've come up with a couple of solutions myself but I'm not sure if they're appropriate in a work environment;
-
create a file called 'playground' separate from your project to experiment with stuff
-
create a branch called 'Experimenting with feature' and feel free to code without
testing, then delete it when the code works, then test it and write it again in the main
branch. Is this odd? -
forget about TDD altogether for things I don't know how to code, write code then the
test -
write an empty test, have it fail at compilation time, write the code then come up with
test contents later on
I'd appreciate any feedback on my ideas & especially your thoughts on the matter. I've been stuck in a loop of not daring to experiment with code out of fear it'd be inappropriate without writing a test first and being unable to write a test.
答案1
得分: 3
你的第二个建议是我推荐的:
> 创建一个名为“尝试新功能”的分支,随时编写代码而不进行测试,然后在代码可行时将其删除,然后在主分支中进行测试并重新编写。
在TDD领域,这被称为“尖峰”。这个词有时在开发中具有稍有不同的意义。但从TDD的角度来看,“尖峰”是指您在没有测试的情况下编写的用于回答问题的代码,例如“getBoundingClientRect
如何工作”或更一般地说,“照片标记代码可能是什么样子的”。
如果觉得将代码丢弃并重新进行TDD测试感到奇怪,那么可以参考文章《如何使用尖峰解决未知问题进行TDD》中提供的一些原因(链接:https://qualitycoding.org/spike-solution/):
> - 它使您在实验时摆脱了编写清晰代码的任何限制。快速而肮脏是游戏的名字。只需快速编码。
> - 它避免创建没有单元测试覆盖的生产代码。一旦我们从尖峰中获得所需的答案,就可以在真正的代码中重新开始TDD周期,利用我们学到的东西。
> - 它防止您编写仅模仿尖峰代码的测试。随着测试的积累和重构,生产代码可能与尖峰解决方案大相径庭。
英文:
Your second suggestion is what I would recommend:
> create a branch called 'Experimenting with feature' and feel free to code without testing, then delete it when the code works, then test it and write it again in the main
branch.
In TDD circles this is called a "spike". That word is sometimes used in development with a slightly different meaning. But in a TDD sense a "spike" is code you write without tests to answer a question, such as "how does getBoundingClientRect
work" or more generally "what could photo-tagging code look like?"
If it feels strange to throw the code away and TDD it again, here are some reasons given in the article How to TDD the Unknown with a Spike Solution:
> - It frees you from any constraints to write clear code while you’re experimenting. Quick-and-dirty is the name of the game. Just hack.
> - It avoids creating production code not covered by unit tests. Once we have the answers we need from our spike, we can start the TDD cycle back in the real code, using what we learned.
> - It keeps you from writing tests that merely mimic the spiked code. As you build up tests and refactor, the production code may assume a radically different shape from the spike solution.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论