应该在哪里你单元测试验证?单元测试

2023-09-06 09:17:36 作者:剧の末

这是困扰我一段时间了。我把我所有的验证在服务层。然而,当我的单元测试,我通常通过操作方法的一切。然后该进入哪些包含验证我的服务层。

This is bothering me for some time now. I put all my validation in a service layer. However when I unit test I usually pass everything through action method. Then that goes into my service layer what contains the validation.

所以,我不知道现在如果说是做最好的方法。因为通常他们说你应该只是测试方法。

So I am not sure now if that is the best way to do it. Since usually they say you should just test that method.

那么你们怎么想的?

推荐答案

如果你传递的一切通过你的行动请求,那么它听起来就像你正在做集成测试。

If you're passing everything through your action request then it sounds like you are doing integration testing.

如果你正在做的(单元)测试,那么你应该检测单位。在这种情况下,你会通过在所有需要你的服务层(模拟)的行动要求的数据。

If you are doing (unit) testing then you should be testing units. In this case you would pass in all the data required to your service layer to (simulate) the action request.

您应该小样传递给业务层对象,通过它,然后断言预期的结果对你实际得到了什么回来了。

You should mock up the object that is passed to the service layer, pass it and then Assert the expected results against what you actually got back.

修改

,正如此外,它是有伟大的端至端,或集成,测试,因为它证明了(处理)的工作原理。

Just as an addition, it's great to have the end-to-end, or integration, tests because it proves that the (process) works.

不过,你必须有单元测试,因为他们将测试的各个组件,将零你在缺陷快于终端到终端的测试能够或者愿意。

However, you have to have the unit tests because they will test the individual components and will zero you in on defects quicker than end-to-end tests can or will.