美文网首页程序员
1-2 Abstract Test Assertions int

1-2 Abstract Test Assertions int

作者: 香草拿铁去冰不加糖 | 来源:发表于2019-03-28 10:17 被阅读0次

    此系列是react-test-library作者写的一系列关于测试的知识点。对于如何写测试,如何写好测试很有帮助

    Abstract Test Assertions into a JavaScript Assertion Library

    Let’s add a simple layer of abstraction in our simple test to make writing tests easier. The assertion library will help our test assertions read more like a phrase you might say which will help people understand our intentions better. It will also help us avoid unnecessary duplication.

    提取码:n8ma
    

    观看视频

    Code

    Transcript

    We're pulling sum and subtract from this math module, and sum has a bug.

    assertion-library.js

    const {sum, subtract} = require('../math')
    

    We can reveal that bug with a simple test.

    result = sum(3, 7)
    expected = 10
    if (result !== expected) {
      throw new Error(`${result} is not equal to ${expected}`)
    }
    

    When we run this file with node, that assertion is throwing an error.

    image

    Also, this code is pretty imperative. It'd be nice to write a little abstraction to make it read a little nicer. Let's go ahead and write a simple abstraction to encapsulate this assertion. I'll create a function down here called expect. That's going to accept an actual.

    function expect(actual){
    
    }
    

    We are going to return an object that has some assertions on it. Our first one here is going to be toBe, and that's going to take an expected value.

    function expect(actual) {
      return {
        toBe(expected) {
        }
      }
    }
    

    We're going to say if the actual is not equal to the expected value, then we'll throw a new Error that says, "Actual is not equal to expected."

    function expect(actual) {
      return {
        toBe(expected) {
          if (actual !== expected) {
            throw new Error(`${actual} is not equal to ${expected}`)
          }
        }
      }
    }
    

    We can take this duplicated code, remove that and replace it with:

    result = sum(3, 7)
    expected = 10
    expect(result).toBe(expected)
    

    If we run that now, we are going to get the same error message. This function is like an assertion library. Fundamentally, it takes an actual value, and then it returns an object that has functions for different assertions that we can make on that actual value.

    Here, we have toBe. We could also have a twoEqual, now it'd take an expected value, and maybe do a deep equality check.

    We could also have a toBeGreaterThan or toBeLessThan, and then it could take an expected value. There're all kinds of assertions that we could add to our little assertion library here to make writing our test a little easier.

    github地址

    相关文章

      网友评论

        本文标题:1-2 Abstract Test Assertions int

        本文链接:https://www.haomeiwen.com/subject/tubfbqtx.html