测试接口有意义吗?
我是 TDD 新手,但我不确定为什么要测试接口?
下面的代码有意义吗?
public interface IInterface
{
int Value { get; }
}
[TestMethod]
public void Test_iinterface_value()
{
var iinterface = mockery.NewMock<IInterface>();
Expect.Once.On(iinterface).GetProperty("Value").Will(Return.Value(10));
Assert.AreEqual(iinterface.Value, 10, "Doh!");
}
I'm new to TDD, but I'm not sure why should I test an interface?
Does the code below make sense??
public interface IInterface
{
int Value { get; }
}
[TestMethod]
public void Test_iinterface_value()
{
var iinterface = mockery.NewMock<IInterface>();
Expect.Once.On(iinterface).GetProperty("Value").Will(Return.Value(10));
Assert.AreEqual(iinterface.Value, 10, "Doh!");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
除非我弄错了,否则您似乎正在创建一个模拟对象,该对象会在要求时返回给定值。然后你询问价值&你明白了(惊讶)。我想说编写这样的测试是没有意义的,除非您正在测试模拟框架本身或者您希望编译器对您玩一些肮脏的把戏。
Unless I am mistaken it looks like you are creating a mock object that returns given value when asked for it. And then you ask for the value & you get it (surprise). I’d say it does not make sense to write such tests, unless you are testing the mocking framework itself or you expect the compiler to play some dirty tricks on you.
这被称为Mockery TDD 反模式。
但是,您可能希望编写一组适用于给定接口的所有实现者的测试,以确保合约得到正确实现。例如,这就是 Grensesnitt 所做的事情。
This is known as the Mockery TDD anti-pattern.
However, you may want to write a set of tests that applies to all implementers of a given interface to ensure that the contracts is being properly implemented. As an example, this is what Grensesnitt does.
测试您的界面主要会导致集成测试,从而有效地测试您的整个堆栈。 UI 中的输入由应用程序逻辑处理,并且通过 UI 响应,您可以看到“全局” - 即您的应用程序是否按预期工作。
在大多数情况下,UI 由标准的、框架提供的类组成,并进行了微小的更改,因此除非您对 UI 框架的功能有疑问,或者正在自己开发一个框架,否则实际上不需要对 UI 类进行单元测试。
如果您没有使用高度自定义的 UI 类,则单元测试应该没有必要,因为另一个原因 - 您的 UI 类中不应该有应用程序逻辑。
Testing your interface mostly results in a integration test, effectively testing your whole stack. Input in the UI is processed by the application logic and by the UI response you can see "the big picture" - i.e. is your application working as expected.
In most cases, UI is composed from standard, framework-provided classes with minor alterations, so unless you have doubts in the capabilities of your UI framework, or are developing one yourself, there is no real need for unit tests on the UI classes.
If you are not using heavily customized UI classes, unit testing should not be necessary for another reason - there should be no application logic in your UI classes.
为接口创建模拟背后的想法是测试另一个对象(“被测对象”),该对象需要正确创建或初始化该模拟。创建一个模拟只是为了测试模拟本身,恕我直言,似乎错过了单元测试的要点。
然而,我可以想到一种情况,为接口编写测试可能是有意义的。如果您编写单元测试不仅仅是为了测试目的,而且还为了了解如何使用某些东西,那么这样的“接口测试”可能会演示如何使用
IInterface
。当然,当你有一个更复杂的界面并且其用法不那么明显时,这是更合理的。The idea behind creating a mock for an interface is to test another object (the "object under test"), which needs this mock to be created or initialized properly. Creating a mock only for testing the mock itself IMHO seems to miss the point of unit testing.
However, I can think of one case where writing a test for an interface perhaps makes sense. If you are writing unit tests not just for testing purpose only, but also for having a synopsis how to use something, then such a "test for an interface" may demonstrate how to use
IInterface
. Of course, that is more reasonable when you have a more complicated interface where its usage is not so obvious.测试接口没有意义。测试模拟对象也没有意义——除非您使用模拟对象来测试抽象类(c# 定义中的抽象)的实现方法。
Testing interfaces does not make sense. Testing a mock object does not make sense either -- unless you use the mock object to test an implemented method of an abstract class (abstract in the c# definition).