简体   繁体   English

我对该方法进行单元测试有哪些选择?

[英]What Are My Options For Unit-Testing This Method?

My Ray module: 我的Ray模块:

define(['Util', 'Vector3f'], function (Util, Vector3f) {
  var Ray = {}
  Ray.o = null;
  Ray.d = null;
  Ray.depth = 0;
  Ray.mint = 0.03;
  Ray.maxt = null;
  return Ray;
});

My unit test: 我的单元测试:

describe(".moveAlong(Number t)", function(){
  it("returns a point at distance t in the direction of the ray", 
  function(){
    expect(4).toBe(null); //unimplemented unit test always fails
  });
});

Ray.o is the ray's origin. Ray.o是射线的起源。 Ray.d is the ray's direction. Ray.d是射线的方向。 I want Ray.moveAlong(t) to return a point q such that q = o + d*t. 我希望Ray.moveAlong(t)返回一个点q,使得q = o + d * t。

My understanding of unit-testing is that if I actually include my Vector3f module in my unit-test so that I can give the Ray an origin and a direction, what I'm actually doing it integration-testing. 我对单元测试的理解是,如果我将Vector3f模块实际包含在单元测试中,以便为Ray提供起点和方向,那么我实际上是在进行集成测试。 But I'll need the add() and mulScalar() methods from my Vector3f module in order to calculate ray.d + ray.d*t in moveAlong(t). 但是我将需要从Vector3f模块中获取add()和mulScalar()方法,以便在moveAlong(t)中计算ray.d + ray.d * t。

What are my options for handling my Vector3f-dependency here? 在这里处理我的Vector3f依赖项有哪些选择? I don't see how I can reasonably stub it out, but stubbing out dependencies and testing only one method at a time is the point of unit-testing. 我没有看到如何合理地对其进行存根,但是对存根进行存根并一次仅测试一种方法是单元测试的重点。

Unit-testing options: 单元测试选项:

First option: 第一种选择:
Just pass in Vector3f objects for Ray.o, Ray.d 只需将Vector3f对象传递给Ray.o,Ray.d

Pros: 优点:
- Easy. - 简单。

Cons: 缺点:
- Preserves dependency in tests. -保留测试中的依赖性。
- Updates to components not under this test (Vector3f) may require updates to this test. -对不在此测试中的组件(Vector3f)进行更新可能需要对该测试进行更新。

Second Option: 第二种选择:
Create stub Vector3fs, each implementing only the add(Vector3f v), mulScalar(Number t) methods of the Vector3f module. 创建存根Vector3fs,每个存根仅实现Vector3f模块的add(Vector3f v),mulScalar(Number t)方法。 This is super easy in Javascript, because references are typeless, and any object that has the right methods may substitute for the "right" object. 这在Javascript中非常容易,因为引用是无类型的,并且具有正确方法的任何对象都可以替代“正确”对象。 Which makes me think that trying to shoe-horn OOP into this project is a bad idea, but I'm not really sure how else to approach this and that's a topic for another question. 这使我认为尝试将OOP引入该项目是一个坏主意,但是我不确定如何解决这个问题,这是另一个问题。

Pros: 优点:
- Breaks dependency between Ray and Vector3f in testing code, so that further changes to Vector3f will not result in Ray failing unit tests, which cuts down on the number of failed tests to be inspected if something breaks Vector3f. -打破了测试代码中Ray和Vector3f之间的依赖关系,因此,对Vector3f的进一步更改将不会导致Ray单元测试失败,如果某些东西破坏了Vector3f,则可以减少要检查的失败测试的数量。

Cons: 缺点:
- More code in tests. -测试中有更多代码。
- If Vector3f is altered in a way that lets it pass all its unit tests, but breaks functionality of Ray, then we will not see it in the Ray unit tests, either, because the dependency on Vector3f has been broken. -如果更改Vector3f的方式使其可以通过其所有单元测试,但破坏了Ray的功能,则我们也不会在Ray单元测试中看到它,因为对Vector3f的依赖性已被破坏。

I'm not sure that last con is a problem - if Vector3f is passing all its unit tests, then it is obeying its contract with the other components of the system, so we shouldn't ever see a situation where Vector3f's tests all pass but it causes other components break. 我不确定最后一个缺点是否是一个问题-如果Vector3f通过所有的单元测试,那么它正在遵守与系统其他组件的合同,因此我们永远都不会看到Vector3f的测试全部通过的情况,但它会导致其他组件损坏。 Further, that's an integration-level issue, not a unit-level issue. 此外,这是集成级别的问题,而不是单元级别的问题。

I think the second option is the way to go. 我认为第二种选择是要走的路。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM