Error with SpyObj how to reassign the value of spy

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

Error with SpyObj how to reassign the value of spy

问题

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

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

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

以下是我的代码:

  1. describe('Side Pane', () => {
  2. const state = cleanState<{
  3. component: Component,
  4. store: Store<{}>
  5. }>(() => {
  6. spyOn(Flags, 'ActionsEnabled').and.returnValue(true);
  7. setupHybridModule({
  8. imports:
  9. [ActionsModule],
  10. providers: []
  11. });
  12. const store = get(Store);
  13. return {component: bootstrap(Actions), store};
  14. }, beforeEach);
  15. it('expects component to be hidden by default', () => {
  16. // 我想要禁用该标志并进行测试
  17. spyOn(Flags, 'ActionsEnabled').and.returnValue(false); // 错误:<spyOn>:ActionsEnabled已经被间谍监视了
  18. expect(hasEl('.actions-header')).toBeFalse();
  19. });
  20. });

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

英文:

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:

  1. describe(&#39;Side Pane&#39;, () =&gt; {
  2. const state = cleanState&lt;{
  3. component: Component,
  4. store: Store&lt;{}&gt;,
  5. }&gt;(() =&gt; {
  6. spyOn(Flags, &#39;ActionsEnabled&#39;).and.returnValue(true);
  7. setupHybridModule({
  8. imports:
  9. [ActionsModule],
  10. providers: []
  11. });
  12. const store = get(Store);
  13. return {component: bootstrap(Actions), store};
  14. }, beforeEach);
  15. it(&#39;expects component to be hidden by default&#39;, () =&gt; {
  16. // I want to disable the flag and test
  17. spyOn(Flags, &#39;ActionsEnabled&#39;).and.returnValue(false); // ERROR: &lt;spyOn&gt; : ActionsEnabled has already been spied upon
  18. expect(hasEl(&#39;.actions-header&#39;)).toBeFalse();
  19. });

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 !!:

  1. describe(&#39;Side Pane&#39;, () =&gt; {
  2. // !! Declare the actionsEnabledSpy that will be a spy later on.
  3. let actionsEnabledSpy: jasmine.Spy;
  4. const state = cleanState&lt;{
  5. component: Component,
  6. store: Store&lt;{}&gt;,
  7. }&gt;(() =&gt; {
  8. // !! Assign the spyOn to the actionsEnabledSpy
  9. actionsEnabledSpy = spyOn(Flags, &#39;ActionsEnabled&#39;).and.returnValue(true);
  10. setupHybridModule({
  11. imports:
  12. [ActionsModule],
  13. providers: []
  14. });
  15. const store = get(Store);
  16. return {component: bootstrap(Actions), store};
  17. }, beforeEach);
  18. it(&#39;expects component to be hidden by default&#39;, () =&gt; {
  19. // I want to disable the flag and test
  20. // !! Over here, use the actionsEnabledSpy to change the value
  21. actionsEnabledSpy.and.returnValue(false);
  22. // !! You can also get other methods on actionsEnabledSpy like reset() to reset the spy (clear out all the times it was called).
  23. expect(hasEl(&#39;.actions-header&#39;)).toBeFalse();
  24. });

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:

确定