存根对象命名约定

发布于 2025-02-08 21:50:04 字数 1804 浏览 1 评论 0原文

我需要对基于4个参数组合对数组进行分类的函数执行一套单元测试。功能的性质使其具有很多边缘案例,我需要测试所有这些案例。

为此,我计划为功能的输入和输出创建一组存根阵列。但是我在命名这些存根对象时遇到了麻烦。

我的第一个想法是每个测试用例有2个存根,一个用于初始数组,另一个用于排序的数组。也许要做类似的事情:

//test case: should prioritize duration over raw priority

export const DifferentPriorityAndDurationStub = () => {
  const t1 = {priority: 1, duration: 20};
  const t2 = {priority: 2, duration: 30};
  return [t1, t2];
};

export const DifferentPriorityAndDurationStubResult= () => {
  const t1 = {priority: 1, duration: 20};
  const t2 = {priority: 2, duration: 30};
  return [t2, t1];
};

然后,在实际测试中:

describe('Custom Sorter', () => {
    it('should prioritize duration over raw priority', () => {
      const originalArray = DifferentPriorityAndDurationStub();
      const result = DifferentPriorityAndDurationStubResult();
      expect(customSorter.sort( originalArray )).toEqual(result );
    });
  });

我的方法是,如果某人需要在功能上工作,则测试代码不会准确地告诉他们输入中的内容以及输出中的内容,他们需要转到存根文件。我认为这使得在这种特定情况下,很难理解该排序功能应如何工作。

我想到的另一种方式是类似的:

export const Priority1Duration20 = () => {
  return {priority: 1, duration: 20};
};

export const Priority2Duration30 = () => {
  return {priority: 2, duration: 30};
};

在我的测试中做这样的事情:

describe('Custom Sorter', () => {
    it('should prioritize duration over raw priority', () => {
      const Priority1Duration20 = Priority1Duration20();
      const Priority2Duration30 = Priority2Duration30();
      expect(customSorter.sort( [Priority1Duration20, Priority2Duration30] ))
            .toEqual([Priority2Duration30,Priority1Duration20]);
    });
  });

我认为,如果某人需要了解代码的工作原理,这似乎更可读。但这也让我担心,我需要更改存根文件和测试文件才能更改或修复某些测试用例。在第一种方法中,我只需要修复存根,那就是全部。

有什么想法吗?您将如何处理?

I need to perform a suite of unit tests to a function that sorts an array based on a combination of 4 parameters. The nature of the functions makes it so that it has a lot of edge cases and I need to test all of them.

For this, I'm planning to create a set of Stub arrays both for the inputs and the outputs of the function. But I'm having trouble naming these stub objects.

My first thought was having 2 stubs for each test case, one for the initial array and other for the sorted array. Maybe doing something like this:

//test case: should prioritize duration over raw priority

export const DifferentPriorityAndDurationStub = () => {
  const t1 = {priority: 1, duration: 20};
  const t2 = {priority: 2, duration: 30};
  return [t1, t2];
};

export const DifferentPriorityAndDurationStubResult= () => {
  const t1 = {priority: 1, duration: 20};
  const t2 = {priority: 2, duration: 30};
  return [t2, t1];
};

Then, in the actual test:

describe('Custom Sorter', () => {
    it('should prioritize duration over raw priority', () => {
      const originalArray = DifferentPriorityAndDurationStub();
      const result = DifferentPriorityAndDurationStubResult();
      expect(customSorter.sort( originalArray )).toEqual(result );
    });
  });

My problem with this approach is that if someone needs to work on the function, the test's code would not tell them exactly what is in the inputs and what is in the output, they would need to go to the stubs file. I think this makes it difficult to understand how the sort functions should work in this specific case.

The other way I thought about was something like:

export const Priority1Duration20 = () => {
  return {priority: 1, duration: 20};
};

export const Priority2Duration30 = () => {
  return {priority: 2, duration: 30};
};

And in my test do something like this:

describe('Custom Sorter', () => {
    it('should prioritize duration over raw priority', () => {
      const Priority1Duration20 = Priority1Duration20();
      const Priority2Duration30 = Priority2Duration30();
      expect(customSorter.sort( [Priority1Duration20, Priority2Duration30] ))
            .toEqual([Priority2Duration30,Priority1Duration20]);
    });
  });

I think this seems more readable if someone needs to understand how the code works. But it also worries me that I need to make changes to the Stubs file and the test file to change or fix certain test cases. In the first approach I'd just need to fix the stub and that would be all.

Any ideas? how would you approach this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文