如何将 Jasmine 的 'toHavenBeenCalledWith()' 与多行文字字符串匹配

How to match Jasmine's 'toHavenBeenCalledWith()' with a multiline literal string

我有一个方法,它接受一个用多行文字字符串编写的脚本,并在其他几个方法中调用它。它看起来像:

myMethod(`
        thisIsMyScript();
        itDoesThings();
    `);

要测试它是否已使用我正在执行的预期脚本调用:

  it('should call script', async function () {
    const scriptMock = `
            thisIsMyScript();
            itDoesThings();
`;
    spyOn(component, 'myMethod');

    await component.testedMethod();
    
    expect(component.myMethod).toHaveBeenCalledWith(
      scriptMock
    );
});

但是我的测试失败了,因为两个字符串都不匹配:

   Expected spy myMethod to have been called with:
     [ '
            thisIsMyScript();
            itDoesThings();
   ' ]
   but actual call was:
     [ '
            thisIsMyScript();
            itDoesThings();
           ' ].

我应该如何处理这些情况?

编辑:按照AliF50的解决方案,这是我必须做的调整(粘贴在这里而不是评论更好的代码格式)以防万一有人需要

const arg = myMethodSpy.calls.mostRecent().args[0].toString();   
// it won't recognize this as a string else.
expect(arg.replace(/ +/g, '')).toBe(scriptMock.replace(/ +/g, ''));  
// it was giving me problems with the inner spaces else

我会得到参数的句柄并断言它包含字符串。

像这样:

  it('should call script', async function () {
    const scriptMock = `
            thisIsMyScript();
            itDoesThings();
`;
    const myMethodSpy = spyOn(component, 'myMethod');

    await component.testedMethod();
    
    // we can get the arguments as an array
    // since there is only one argument, it will be the 0th one in the array
    const arg = myMethodSpy.calls.mostRecent().args[0];
    expect(arg.includes('thisIsMyScript();')).toBeTrue();
    expect(arg.includes('itDoesThings();')).toBeTrue();
    // this might work too if we trim both strings
    expect(arg.trim()).toBe(scriptMock.trim());
});