有人可以提供存根和驱动程序的实际示例吗?

发布于 2024-08-12 00:43:51 字数 61 浏览 14 评论 0原文

我需要一些关于自上而下和自下而上的测试方法的存根和驱动程序的实际示例。我这里不需要代码。只是基于场景的示例。

I need some practical examples of stubs and drivers with respect to top-down and bottom-up approaches to testing. I don't require code here. Just the scenario based examples.

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

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

发布评论

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

评论(1

耳根太软 2024-08-19 00:43:51

驱动程序是一组测试类的接口(方法、属性、构造函数等)的测试。

存根是一个假对象,充当数据库或记录器等其他功能的替代品。

模拟是一个带有断言的假对象。

以下是使用模拟对象的测试示例。如果你取出断言,它就变成了一个存根。总的来说,这些类型的测试是驱动程序,因为它们运用对象的方法和属性。

这是示例:

[Test]
public void TestGetSinglePersonWithValidId()
{
    // Tell that mock object when the "GetPerson" method is called to 
    // return a predefined Person
    personRepositoryMock.ExpectAndReturn("GetPersonById", onePerson, "1");
    PersonService service = new PersonService(
        (IPersonRepository) personRepositoryMock.MockInstance);
    Person p = service.GetPerson("1");
    Assert.IsNotNull(p);
    Assert.AreEqual(p.Id, "1");
}

http://www. zorched.net/2007/03/10/mocking-net-objects-with-nunit/

A driver is a set of tests that test the interface of your class (methods, properties, constructor, etc).

A stub is a fake object that acts as a stand-in for other functionality like a database or a logger.

A mock is a fake object that has asserts in it.

Following is an example of a test using a mock object. If you take out the asserts, it becomes a stub. Collectively, these kinds of tests are drivers, because they exercise the methods and properties of your object.

Here is the example:

[Test]
public void TestGetSinglePersonWithValidId()
{
    // Tell that mock object when the "GetPerson" method is called to 
    // return a predefined Person
    personRepositoryMock.ExpectAndReturn("GetPersonById", onePerson, "1");
    PersonService service = new PersonService(
        (IPersonRepository) personRepositoryMock.MockInstance);
    Person p = service.GetPerson("1");
    Assert.IsNotNull(p);
    Assert.AreEqual(p.Id, "1");
}

http://www.zorched.net/2007/03/10/mocking-net-objects-with-nunit/

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