Error with SpyObj how to reassign the value of spy

huangapple go评论68阅读模式
英文:

Error with SpyObj how to reassign the value of spy

问题

我对Jasmine相对不熟悉,所以请原谅我的愚蠢问题。

我尝试只为一个it块覆盖间谍的值,但遇到了问题。

我看到了ERROR:<spyOn>:ActionsEnabled已经被间谍监视了错误。我该如何修复这个问题?

以下是我的代码:

describe('Side Pane', () => {
  const state = cleanState<{
    component: Component,
    store: Store<{}>
  }>(() => {
    spyOn(Flags, 'ActionsEnabled').and.returnValue(true);

    setupHybridModule({
      imports:
        [ActionsModule],
      providers: []
    });

    const store = get(Store);

    return {component: bootstrap(Actions), store};
  }, beforeEach);


  it('expects component to be hidden by default', () => {
    // 我想要禁用该标志并进行测试
    spyOn(Flags, 'ActionsEnabled').and.returnValue(false); // 错误:<spyOn>:ActionsEnabled已经被间谍监视了
    expect(hasEl('.actions-header')).toBeFalse();
  });
});

对于我做错了什么,我会非常感激一些见解。

英文:

I'm fairly new to Jasmine so excuse my silly question.

I'm trying to override the value of the spy only for one it block and I'm hitting issues.

I'm seeing ERROR: &lt;spyOn&gt; : ActionsEnabled has already been spied upon error. How do i fix this?

Here is my code:

describe(&#39;Side Pane&#39;, () =&gt; {
  const state = cleanState&lt;{
    component: Component,
    store: Store&lt;{}&gt;,
  }&gt;(() =&gt; {
    spyOn(Flags, &#39;ActionsEnabled&#39;).and.returnValue(true);

    setupHybridModule({
      imports:
          [ActionsModule],
      providers: []
    });

    const store = get(Store);

    return {component: bootstrap(Actions), store};
  }, beforeEach);


  it(&#39;expects component to be hidden by default&#39;, () =&gt; {
    // I want to disable the flag and test
    spyOn(Flags, &#39;ActionsEnabled&#39;).and.returnValue(false); // ERROR: &lt;spyOn&gt; : ActionsEnabled has already been spied upon
    expect(hasEl(&#39;.actions-header&#39;)).toBeFalse();
  });

I'd appreciate some insights on what I'm doing wrong.

答案1

得分: 1

以下是代码中需要翻译的部分:

  1. "It's one of the quirks of Jasmine where if something has been spied on, it cannot be spied upon again. To fix this issue, you can assign the spyOn to a 'global' variable within the describe block and then use this 'global' variable to change the value."

  2. "Follow comments with !!:"

  3. "describe('Side Pane', () => {"

  4. "// !! Declare the actionsEnabledSpy that will be a spy later on."

  5. "let actionsEnabledSpy: jasmine.Spy;"

  6. "const state = cleanState<{
    component: Component,
    store: Store<{}>,
    }>(() => {"

  7. "// !! Assign the spyOn to the actionsEnabledSpy"

  8. "actionsEnabledSpy = spyOn(Flags, 'ActionsEnabled').and.returnValue(true);"

  9. "setupHybridModule({
    imports:
    [ActionsModule],
    providers: []
    });"

  10. "const store = get(Store);"

  11. "return {component: bootstrap(Actions), store};
    }, beforeEach);"

  12. "it('expects component to be hidden by default', () => {"

  13. "// I want to disable the flag and test"

  14. "// !! Over here, use the actionsEnabledSpy to change the value"

  15. "actionsEnabledSpy.and.returnValue(false);"

  16. "// !! You can also get other methods on actionsEnabledSpy like reset() to reset the spy (clear out all the times it was called)."

  17. "expect(hasEl('.actions-header')).toBeFalse();
    });"

英文:

It's one of the quirks of Jasmine where if something has been spied on, it cannot be spied upon again. To fix this issue, you can assign the spyOn to a "global" variable within the describe block and then use this "global" variable to change the value.

Follow comments with !!:

describe(&#39;Side Pane&#39;, () =&gt; {
  // !! Declare the actionsEnabledSpy that will be a spy later on.
  let actionsEnabledSpy: jasmine.Spy;
  const state = cleanState&lt;{
    component: Component,
    store: Store&lt;{}&gt;,
  }&gt;(() =&gt; {
    // !! Assign the spyOn to the actionsEnabledSpy
    actionsEnabledSpy = spyOn(Flags, &#39;ActionsEnabled&#39;).and.returnValue(true);

    setupHybridModule({
      imports:
          [ActionsModule],
      providers: []
    });

    const store = get(Store);

    return {component: bootstrap(Actions), store};
  }, beforeEach);


  it(&#39;expects component to be hidden by default&#39;, () =&gt; {
    // I want to disable the flag and test
    // !! Over here, use the actionsEnabledSpy to change the value
    actionsEnabledSpy.and.returnValue(false);
    // !! You can also get other methods on actionsEnabledSpy like reset() to reset the spy (clear out all the times it was called).
    expect(hasEl(&#39;.actions-header&#39;)).toBeFalse();
  });

huangapple
  • 本文由 发表于 2023年7月18日 11:55:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76709433.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定