测试模块中定义的私有函数是否使用来自同一模块的公共函数使用正确的args进行调用(使用Jest)

时间:2016-12-23 10:17:44

标签: unit-testing jestjs

我有一个项目,我正在编写Jest测试。我有一个特定的模块,如下所示:

export default function main(arg) {
  /* do some stuff with arg */
  sub(arg);
}

function sub(arg) {
  return;
}

我想用正确的参数测试主调用sub。我试过这个:

import main from './tiny';

describe('main function', () => {
  it('calls sub with correct arguments', () => {
    let sub = jest.fn();
    main('hello world');
    expect(sub).toHaveBeenCalled();
    expect(sub).toHaveBeenCalledWith('hello world');
  });
});

但是这个测试总是失败:

main function › calls sub with correct arguments

    expect(jest.fn()).toHaveBeenCalled()

我是单位测试和嘲笑的新手,所以我确信这很容易,但我完全陷入困境。请帮忙!

1 个答案:

答案 0 :(得分:0)

如果有人发现这个,我使用以下(有些难看)mods:

导出该函数并从exports中调用它:

Require Import Classical.

Lemma helper (z : R) (E : R -> Prop) :
    (forall y, y > z -> ~ E y) -> is_upper_bound E z.
Proof.
  intros H x Ex.
  destruct (Rle_dec x z).
  - assumption.
  - specialize (H x (Rnot_le_gt x z n)); contradiction.
Qed.

Lemma supremum :forall E:R -> Prop,
  (exists l : R,is_upper_bound E l) ->
  (exists x : R, E x) ->
  {m:R | is_lub E m /\ (forall x:R, x<m -> exists y:R, E y /\ y > x)}.
Proof.
  intros E Hbound Hnonempty.
  pose proof (completeness E Hbound Hnonempty) as [m Hlub].
  clear Hbound Hnonempty.
  exists m. split; auto.
  intros x Hlt.
  assert (~ (forall y, y > x -> ~ E y)) as Hclass.
    intro Hcontra; apply helper in Hcontra.
    destruct Hlub as [Hup Hle].
    specialize (Hle x Hcontra).
    apply Rle_not_lt in Hle; contradiction.
  (* classical part starts here *)
  apply not_all_ex_not in Hclass as [y Hclass]; exists y.
  apply imply_to_and in Hclass as [Hyx HnotnotEy].
  now apply NNPP in HnotnotEy.
Qed.

导入导出对象,用模块覆盖该方法:

export default function main(arg) {
  /* do some stuff with arg */
  exports.sub(arg);
}

export function sub(arg) {
  return;
}