如果我的考试中有验证,期望是否多余? [英] Is Expectations redundant if I have Verifications in my test?

查看:83
本文介绍了如果我的考试中有验证,期望是否多余?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对期望和验证的目的和区别感到困惑.例如

I'm confused as to the purpose of and difference between expectations and verifications. E.g.

@Tested FooServiceImpl fooService;
@Injectable FooDao fooDao;

@Test
public void callsFooDaoDelete() throws Exception {
    new Expectations() {{
        fooDao.delete(withEqual(1L)); times = 1;
    }};

    fooService.delete(1L);

    new Verifications() {{
        Long id;
        fooDao.delete(id = withCapture()); times = 1;
        Assert.assertEquals(1L, id);
    }};
}

首先,请让我知道该测试的书写/考虑是否周密.

First of all, please let me know if this test is poorly written/thought out.

第二,我的问题:期望部分对我来说似乎是多余的,我无法提出一个不可能的例子.

Second, my question: the expectations section seems redundant to me, and I can't come up with an example where it wouldn't be.

推荐答案

Expectations的目的是允许测试根据需要为模拟方法和/或构造函数记录预期结果 被测代码.

The purpose of Expectations is to allow a test to record expected results for mocked methods and/or constructors, as needed by the code under test.

Verifications的目的是允许测试对被模拟的代码和/或构造函数进行验证期望的调用

The purpose of Verifications is to allow a test to verify expected invocations to mocked methods and/or constructors, as made by the code under test.

因此,通常情况下,测试不会同时记录来验证相同的期望(其中期望"指定了对模拟方法/构造函数的一组调用,这些调用将在代码执行时发生)进行测试).

So, normally, a test wouldn't both record and verify the same expectation (where an "expectation" specifies a set of invocations to mocked methods/constructors that are expected to occur when the code under test is exercised).

考虑到这一点,示例测试将如下所示:

With that in mind, the example test would look like this:

@Tested FooServiceImpl fooService;
@Injectable FooDao fooDao;

@Test
public void callsFooDaoDelete() throws Exception {
    fooService.delete(1L);

    new Verifications() {{ fooDao.delete(1L); }};
}

这篇关于如果我的考试中有验证,期望是否多余?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆