用 Jasmine 监视私有变量的属性/函数

Gre*_*Guy 6 unit-testing private mocking jasmine typescript

我有一个基于它读取的文件具有可变功能的函数,该函数通过Map它保存在内存中的一个来控制:

file1.ts

function f1(x: number): number {
  // do far-reaching things
  return 1;
}

function f2(x: number): number {
  // do different far-reaching things
  return 2;
}

function f3(x: number): number {
  // do still-different far-reaching things
  return 3;
}

const myMap: Map<string, (number) => number> = new Map<string, () => void>([
  ['key1', f1],
  ['key2', f2],
  ['key3', f3],
]

export function doThing(filename: string): number {
  // open file, make some database calls, and figure out the name of a key
  // ...
  let fileToExecute = myMap.get(key);
  return fileToExecute(someValueDerivedFromFile);
}
Run Code Online (Sandbox Code Playgroud)

f1, f2, 和f3所有比这里显示的要多得多,并且每个都需要大量的模拟才能成功测试。

随着代码的发展和用例的不断发展,基于不断扩展的输入集,可能需要调用任意数量的函数。doThing()它很复杂,并且从许多不同的来源获取信息,包括给定文件的内容和数据库,这有助于它选择要执行的文件。从客户的角度来看,这doThing()是它唯一关心的功能。因此,它是export该文件唯一的ed。

我正在尝试测试该机制,以doThing()确定key它应该使用什么。我不想嘲笑f1, f2f3特别是 - 我想展示更多的选项,由我嘲笑的其他东西指出doThing()。但是,要检查它是否正在调用正确的假方法,我需要弄清楚它正在调用哪个假方法。我尝试的解决方案使用类型转换来尝试myMap从文件中提取私有文件,然后监视其get()方法:

file1.spec.ts

import * as file1 from '../src/file1'
...
it("calls the correct fake method", () => {
  // lots of other mocks
  let spies = [
    jasmine.createSpy('f1spy').and.returnValue(4),
    jasmine.createSpy('f2spy').and.returnValue(5),
    jasmine.createSpy('f3spy').and.returnValue(6),
    ...
  ]
  let mockMap = spyOn((file1 as any).myMap, 'get').and.callFake((key) => {  // this fails
    var spy;
    switch(key) {
      case 'key1': spy = spies[0]; break;
      case 'key2': spy = spies[1]; break;
      case 'key3': spy = spies[2]; break;
      ...
    }
    return spy;
  }

  result = file1.doThing(...);

  expect(spies[0]).not.toHaveBeenCalled();
  expect(spies[1]).toHaveBeenCalledWith(7);
  expect(spies[2]).not.toHaveBeenCalled();
});
Run Code Online (Sandbox Code Playgroud)

但是,我在上面的注释行中收到一个错误:Error: <spyOn> : could not find an object to spy upon for get(). 经过进一步调查(即逐步调试器),结果file1我导入的对象只有doThing(),并且没有任何其他私有变量。

我如何在这里成功模拟键值转换 - 这意味着,在这种情况下,监视私有变量的属性,以便我可以在正确的位置找到我的间谍?如果可能,要么myMap完全更换,要么更换myMap.get()

Max*_*rok 3

总体思路:使用rewire.

使用rewire,我们将用函数覆盖您的私有函数spy

但是,您的const myMap需求需要修改。因为当你这样做时['key1', f1]- 它会存储 的当前实现f1,因此我们无法在myMap初始化后覆盖它。克服这个问题的方法之一是使用['key1', args => f1(args)]。这样,它就不会存储f1函数,只存储调用它的包装器。您可以通过使用apply()或来实现相同的效果call()

实施示例:

file1.ts:

function f1(): number {
  // do far-reaching things
  return 1;
}

const myMap: Map<string, (x: number) => number> = new Map([
  ['key1', (...args: Parameters<typeof f1>) => f1(...args)],
]);

export function doThing(): number {
  const key = 'key1';
  const magicNumber = 7;
  const fileToExecute = myMap.get(key);
  return fileToExecute(magicNumber);
}
Run Code Online (Sandbox Code Playgroud)

file1.spec.ts:

import * as rewire from 'rewire';

it('calls the correct fake method', () => {
  const spies = [jasmine.createSpy('f1spy').and.returnValue(4)];

  const myModule = rewire('./file1');

  myModule.__set__('f1', spies[0]);

  myModule.doThing();

  expect(spies[0]).toHaveBeenCalledWith(7);
});
Run Code Online (Sandbox Code Playgroud)

为了rewire与 typescript 一起使用,您可能需要使用 babel 等。

为了概念证明,我将编译它:

function f1(): number {
  // do far-reaching things
  return 1;
}

const myMap: Map<string, (x: number) => number> = new Map([
  ['key1', (...args: Parameters<typeof f1>) => f1(...args)],
]);

export function doThing(): number {
  const key = 'key1';
  const magicNumber = 7;
  const fileToExecute = myMap.get(key);
  return fileToExecute(magicNumber);
}
Run Code Online (Sandbox Code Playgroud)

并运行测试:

import * as rewire from 'rewire';

it('calls the correct fake method', () => {
  const spies = [jasmine.createSpy('f1spy').and.returnValue(4)];

  const myModule = rewire('./file1');

  myModule.__set__('f1', spies[0]);

  myModule.doThing();

  expect(spies[0]).toHaveBeenCalledWith(7);
});
Run Code Online (Sandbox Code Playgroud)

它将成功运行:

Started
.


1 spec, 0 failures
Run Code Online (Sandbox Code Playgroud)

更新

无需修改myMap

file1.spec.ts:

import * as rewire from 'rewire';

it('calls the correct fake method', () => {
  const spies = [
    jasmine.createSpy('f1spy').and.returnValue(4),
    jasmine.createSpy('f2spy').and.returnValue(5),
    // ...
  ];

  const myModule = rewire('./file1');

  const myMockedMap: Map<string, (x: number) => number> = new Map();

  (myModule.__get__('myMap') as typeof myMockedMap).forEach((value, key) =>
    myMockedMap.set(key, value)
  );

  myModule.__set__('myMap', myMockedMap);

  // ...
});
Run Code Online (Sandbox Code Playgroud)