单元测试 断言值为null
by Edd Yerburgh
埃德·耶堡(Edd Yerburgh)
如何通过使用值断言编写更强大的单元测试 (How to write more powerful unit tests by using value assertions)
Unit tests are awesome. Writing unit tests reduces bugs by 40–80%.
单元测试很棒。 编写单元测试可以将错误减少40–80% 。
But you need to do them right. Poorly written unit tests can suffocate a codebase, and cause more problems than they solve.
但是您需要正确地做它们。 编写不佳的单元测试可能会使代码库窒息,并导致更多的问题而不是解决的。
One way to improve your unit tests is to use value assertions.
改善单元测试的一种方法是使用值声明 。
In this article we’ll look at what value assertions are, and how to use them to improve your tests.
在本文中,我们将研究什么是价值断言,以及如何使用它们来改进您的测试。
了解断言 (Understanding assertions)
Assertions are functions that check to make sure the code behaved as we expected.
断言是检查以确保代码行为符合我们预期的函数。
Different languages have different conventions. In JavaScript, it’s common to follow the expect
pattern. This is where you expect
a condition to match a value.
不同的语言有不同的约定。 在JavaScript中,通常遵循expect
模式。 这是您expect
条件与值匹配的地方。
We combine the expect
function with another function called a matcher.
我们将expect
函数与另一个称为matcher的函数结合在一起。
In the example below, we expect
the result of sum(1,1)
to equal 2
. The toBe
matcher checks that the expect value equals 2
.
在下面的示例中,我们expect
sum(1,1)
的结果等于2
。 toBe
匹配器检查期望值等于2
。
expect(sum(1,1)).toBe(2)
If the result of sum(1,1)
equals 2
, the function won’t do anything and the test will pass. If sum(1,1)
doesn’t equal 2
, the function throws an assertion error and the test fails.
如果sum(1,1)
的结果等于2
,则该函数将不执行任何操作,并且测试将通过。 如果sum(1,1)
不等于2
,则该函数将引发断言错误 ,并且测试将失败。
调试断言错误 (Debugging assertion errors)
In test frameworks, assertion errors are formatted to make the message easier to read. Assertion errors let you figure out quickly what went wrong in the test.
在测试框架中,对断言错误进行格式化以使消息更易于阅读。 断言错误使您可以快速找出测试中出了什么问题。
You can see a failing Jest assertion error below:
您可以在下面看到失败的Jest断言错误:
For some reason, sum(1,1)
returned 3
.
由于某些原因, sum(1,1)
返回3
。
If we check the code, we’ll find someone accidentally added b
twice:
如果我们检查代码,就会发现有人不小心将b
加了两次:
function sum(a,b) { return a + b + b}
We can fix the error quickly and get the sum
function working again. The assertion error helped us figure out what went wrong and where.
我们可以快速修复错误,并使sum
函数再次运行。 断言错误有助于我们找出问题所在和出处。
什么是价值主张? (What’s a value assertion?)
A value assertion is an assertion that compares two values.
值断言是将两个值进行比较的断言 。
We just wrote a value assertion:
我们只是写了一个价值主张:
expect(sum(1,1)).toBe(2)
And it generated the assertion error:
它产生了断言错误:
Expected value to be (using ===): 2 Received: 3
还有哪些其他主张? (What other assertions are there?)
Another common assertion is a boolean assertion.
另一个常见的断言是布尔断言 。
A boolean assertion is an assertion that compares two booleans.
布尔断言是将两个布尔值进行比较的断言。
expect(add(1,1) === 2).toBe(true)
This generates a boolean assertion error:
这会产生布尔断言错误:
Expected value to be (using ===): true Received: false
调试值断言 (Debugging a value assertion)
Value assertions throw descriptive assertion errors.
值断言引发描述性断言错误。
When a test fails with a value assertion, you can see why the test is failing. This gives us a clue to what is happening in the code:
当测试失败并带有值断言时,您可以查看测试失败的原因。 这为我们提供了代码中发生情况的线索:
warning: expected 'somevalue' to equal 'some value'
You know what to look for in the code when you see an error like this. Oh, it looks like someone deleted a space by accident.
当您看到这样的错误时,您知道在代码中查找什么。 哦,好像有人不小心删除了一个空格。
Value assertions improve the debuggability (yes that’s a word) of unit tests. From reading the assertion error, you can see what went wrong in the test.
值断言提高了单元测试的可调试性(是的)。 通过读取断言错误,您可以查看测试中出了什么问题。
Let’s look at an assertion error from a boolean assertion:
让我们看一下来自布尔断言的断言错误:
What’s gone wrong?
怎么了?
It takes longer to debug a test with a boolean assertion, because you don’t know what value was returned by the tested code.
使用布尔断言调试测试需要更长的时间,因为您不知道被测试的代码返回了什么值。
This makes boolean assertion errors pretty useless in unit tests.
这使得布尔断言错误在单元测试中几乎没有用。
撰写价值主张 (Writing value assertions)
So we want to write value assertions.
因此,我们要编写值断言。
Most JavaScript testing libraries provide functions to write value assertions.
大多数JavaScript测试库都提供用于编写值断言的函数。
Jest contains tons of useful matchers to create value assertions:
笑话包含大量有用的匹配器,可以创建价值主张:
.toBeGreaterThan(number).toContain(item).toHaveBeenCalled().toHaveProperty(keyPath, value)
呼吁采取行动 (Call to action)
Now you understand the power of value assertions, your tests will improve.
现在您了解了价值主张的力量,您的测试将得到改善。
Get out there and write some debuggable unit tests!
出去写一些可调试的单元测试!
If you enjoyed this article, please give me some claps so more people see it. Thanks!
如果您喜欢这篇文章,请给我一些鼓掌,以便更多的人看到。 谢谢!
单元测试 断言值为null