角茉莉测试 - 子字符串从数字中删除

发布于 2025-01-26 14:53:26 字数 71 浏览 2 评论 0原文

我在单元测试功能方面遇到了一些困难。我在这些事情上是新手,我不知道我能做什么。 我必须对那两个。

有建议吗?谢谢。

I have some difficulties in unit testing a function. I'm new in these things and I don't know what I can do.
I have to those 2 if.

Any advice? Thank you.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如日中天 2025-02-02 14:53:26

我认为您的问题是Jasmine.createspy(Phonenumber);,由于该方法正在测试中,因此我们不应该监视它,而是实际上称其为其实现。

尝试以下内容:

it('should remove outside prefix', () => {
  const dialingOptions = {
    reqLongDistPrefix: 10, reqOutsidePrefix: 11, dialLongDistanceLength: 12
  } as any;
  coreSvc.getCCDef = jasmine.createSpy().and.returnValue({ DialingOptions: dialingOptions });
  let phoneNumber = '1011129999';
  // !! Remove the following line !!
  // component.removeOutsidePrefix = jasmine.createSpy(phoneNumber);
  const modifiedPhoneNumber = component.removeOutsidePrefix(phoneNumber);
  // !! below is not a good assertion, you can remove it
  // expect(Object.keys(dialingOptions).length).toBe(3);
  expect(modifiedPhoneNumber).toBe('insert what you expect it to be here');
});

I think your issue is the jasmine.createSpy(phoneNumber);, since this method is under test, we should not spy on it but rather actually call its implementation.

Try the following:

it('should remove outside prefix', () => {
  const dialingOptions = {
    reqLongDistPrefix: 10, reqOutsidePrefix: 11, dialLongDistanceLength: 12
  } as any;
  coreSvc.getCCDef = jasmine.createSpy().and.returnValue({ DialingOptions: dialingOptions });
  let phoneNumber = '1011129999';
  // !! Remove the following line !!
  // component.removeOutsidePrefix = jasmine.createSpy(phoneNumber);
  const modifiedPhoneNumber = component.removeOutsidePrefix(phoneNumber);
  // !! below is not a good assertion, you can remove it
  // expect(Object.keys(dialingOptions).length).toBe(3);
  expect(modifiedPhoneNumber).toBe('insert what you expect it to be here');
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文