【问题标题】:Unit Testing for the following :以下内容的单元测试:
【发布时间】:2018-12-27 02:18:27
【问题描述】:

试图为以下代码设置单元测试,但我不断得到这个:比较两种不同类型的值。预期的字符串,但收到未定义。作为一个错误

 Test :


     import {prepadSigned} from './utils';

  describe('prepadSigned', () => {test('should prepend `00` to the input      <', () => {
  const str = '-10';
  const actual = prepadSigned(str);
  const expected = '00-10';
    expect(actual).toEqual(expected);
    })

代码:

function prepadSigned(hexStr) {
  const msb = hexStr[0];
  if (msb < '0' || msb > '7') {
    return `00${hexStr}`;
  }
  return hexStr;
} 

我希望单元测试能够通过一些输入/输出。 我不断收到以下信息:

expect(received).toEqual(expected)

Expected value to equal:
  "00-10"
Received:
  undefined

【问题讨论】:

    标签: reactjs unit-testing jestjs


    【解决方案1】:

    除非你写的时候故意漏掉export

    变化

    function prepadSigned(hexStr) {
    ...
    }
    

    export function prepadSigned(hexStr) {
    ...
    }
    

    可以解决问题。

    【讨论】:

    • 测试失败 prepadSigned › 应该输出相同的输入 expect(received).toEqual(expected) 预期值等于:“005” 接收:“5”
    • 我猜你试过const str = '5'const expected = '005' 那里?鉴于您的实施,这不是预期的结果吗?
    • 没关系 - 它奏效了。我基于主要代码: (msb '7') ,如果一个数字在 0 - 7 之间,函数将失败。对吗?我是单元测试新手
    • 根据您的测试用例,成功/失败取决于设置为const strconst expected 的值。测试成功和失败场景是一种很好的做法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-12
    • 1970-01-01
    • 1970-01-01
    • 2020-08-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多