英文:
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: <spyOn> : ActionsEnabled has already been spied upon
error. How do i fix this?
Here is my code:
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', () => {
// I want to disable the flag and test
spyOn(Flags, 'ActionsEnabled').and.returnValue(false); // ERROR: <spyOn> : ActionsEnabled has already been spied upon
expect(hasEl('.actions-header')).toBeFalse();
});
I'd appreciate some insights on what I'm doing wrong.
答案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." -
"Follow comments with !!:"
-
"describe('Side Pane', () => {"
-
"// !! Declare the actionsEnabledSpy that will be a spy later on."
-
"let actionsEnabledSpy: jasmine.Spy;"
-
"const state = cleanState<{
component: Component,
store: Store<{}>,
}>(() => {" -
"// !! Assign the spyOn to the actionsEnabledSpy"
-
"actionsEnabledSpy = 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', () => {"
-
"// 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('.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('Side Pane', () => {
// !! Declare the actionsEnabledSpy that will be a spy later on.
let actionsEnabledSpy: jasmine.Spy;
const state = cleanState<{
component: Component,
store: Store<{}>,
}>(() => {
// !! Assign the spyOn to the actionsEnabledSpy
actionsEnabledSpy = 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', () => {
// 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('.actions-header')).toBeFalse();
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论