asp.net mvc3:您应该使用单元测试测试什么?
我是 TDD 新手。正在开发我的第一个 asp.net mvc3 项目,计划使用单元测试。
我在网上看到了一些示例,教授如何对应用程序进行单元测试。测试路由、控制器、自定义模型绑定器和存储库...
还应该通过单元测试测试什么?我不想过度测试我的应用程序。
这是我在网上找到的样本,我认为它已经过测试了。如果我错了请纠正我。
他编写了 5-6 个测试来测试他的存储库。首先,他创造了 5 个产品。
然后,测试 GetAllProducts,检查 count(),确保其为 5。 然后,测试 GetById(int i),检查 count = 1,并且 name = "sdfsfd" ... ...
我不知道这个单元测试有什么帮助。你列出了自己的清单,你知道会发生什么。
作为一名初学者,请分享您对如何正确进行单元测试的想法。
I am new to TDD. working on my first asp.net mvc3 project, plan to use unit test.
I saw some samples online teaching how to unit test your application. testing routes, controller, custom model binders and repositories ...
what else should be tested with unit test? I dont want to over test my applcation.
here's on sample i found online, i think its over test. correct me if I am wrong.
He wrote 5-6 tests to test his repository. first, He creates 5 products.
then, test GetAllProducts, check count(), making sure its 5.
then, test GetById(int i), check count = 1, and name = "sdfsfd"
...
...
I dont see how this unit test gonna help. you make up your own list, you know what to expect.
please share your thoughts, as a bgeinner, how to unit test properly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您所描述的测试很有用,因为您在测试系统的其他区域时仍然可以使用它们。
在您的示例中,假设您的“创建”函数添加了一个具有 3 个字段的产品,后来您将模型调整为只有 2 个字段,则测试的创建步骤将失败,您将知道任何“创建”代码都需要有待修改。
通常,测试应该检查每个区域的基本功能,以便您知道其他区域是否会在稍后的步骤中破坏此功能。这种方法是回归测试的关键,从长远来看可以节省大量时间。
Tests such as the one you described are useful because you can still use them when you test other areas of the system.
In your example, say your 'create' function adds a product with 3 fields, and later you adjust your model to have only 2 fields, the create step of the test will fail and you'll know that any 'create' code will need to be ammended.
More often than not, the tests should check the basic functions of each area so that you know if other areas break this at a later step. This kind of approach is key to regression testing and can save a lot of time in the long run.