单元测试:如何断言?断言结果返回还是在模拟上调用了一个方法? [英] Unit Tests: How to Assert? Asserting results returned or that a method was called on a mock?

查看:53
本文介绍了单元测试:如何断言?断言结果返回还是在模拟上调用了一个方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出断言的最佳方法,我是否应该创建一个包含我应该返回的对象并检查它是否与预期结果相同的对象?

I am trying to find out the best way to Assert, should I be creating an object with what i should return and check that it has equal to the expected result ?

或者我应该针对模拟运行一个方法以确保该方法被实际调用.

Or should I be running a method against a mock to ensure that the method was actually called.

我已经看到了这两种方式,我想知道是否有人对此有任何最佳做法.

I have seen this done both ways, I wondered if anyone has any best practices for this.

当然,编写单元测试来断言在模拟上调用了一个方法会更快更容易,但更快更容易并不总是最好的方法 - 尽管有时可以.

Of course, it's quicker and easier to write a unit test to assert that a method was called on the mock but quicker and easier is not always the best way - although sometimes it can be.

每个人都断言什么,一个方法已被调用或断言返回的结果?

What does everyone assert on, that a method has been called or assert the results that were returned ?

当然,在单元测试中执行 1 个以上的断言并不是最佳实践,所以也许答案是实际断言结果并调用该方法?所以我会创建 2 个单元测试,1 个用于检查结果,1 个用于检查方法是否被调用.

Of course its not best practice to do more than 1 assert in a unit test so maybe the answer is to actually assert the results and that the method was called ? So I would create 2 unit tests, 1 to check the results and 1 to check that the method was called.

但现在想想,也许这太过分了,如果我得到一个结果,我想我可以假设我的模拟方法被调用了.

But now thinking about this, maybe this is going too far, if I get a result that I suppose I can assume that my mock method was called.

推荐答案

在测试一个方法是否被调用和测试它返回的值之间是另一个可能更重要的测试:它是用正确的参数调用的.

In between testing that a method has been called and testing the value that it returns is another possibly more important test: that it was called with the correct parameters.

如今一个非常常见的情况是您正在编写的方法使用某些 HTTP 库从 REST API 检索数据.您不想在测试中实际发出 HTTP 请求,因此您模拟 HTTP 客户端 get() 方法.一方面,您的模拟可能只是返回一些罐头 JSON 响应,例如(以 Ruby 中的 RSpec 为例):

A very common case these days would a method you're writing that uses some HTTP library to retrieve data from a REST API. You don't want to actually make HTTP requests in your tests so you mock the HTTP client get() method. On the one hand your mock might just return some canned JSON response like (Using RSpec in Ruby as an example):

http_mock.stub(:get).and_return('{result: 5}')

然后测试您是否可以正确解析它并根据响应返回一些正确的值.

You then test that you can properly parse this and return some correct value based on the response.

您还可以测试 HTTP get() 方法是否被调用,但更重要的是测试它是否使用正确的参数调用.对于 API,您的方法可能必须使用查询参数格式化 URL,并且您需要测试它是否正确执行此操作.断言看起来像(再次使用 RSpec):

You could also test that the HTTP get() method is called, but it's more important to test that it's called with the correct parameters. In the case of an API, your method probably has to format a URL with query parameters and you need to test that it does that correctly. The assertion would look something like (again with RSpec):

http_mock.should_receive(:get).with('http:/example.com/some_endpoint?some_param=x')

这个测试确实是上一个测试的先决条件,简单地测试get()被调用并不能确认太多.例如,它会告诉您 URL 的格式是否不正确.

This test is really a prerequisite to the previous test, and simply testing that get() was called wouldn't confirm much. For instance, it would tell you if you were incorrectly formatting the URL.

这篇关于单元测试:如何断言?断言结果返回还是在模拟上调用了一个方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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