javascript - 赛普拉斯 - 从夹具文件中设置本地存储值
问题描述
在开始之前,我必须将以下 JSON 数据设置到我的本地存储中,并且本地存储中的值与夹具文件中的值不完全相同。
requiredJsonData.json:
[
[
1000001,
{
"name": "Marty J",
"callSign": "-",
"flag": null,
"yearBuilt": null
}
],
[
1000002,
{
"name": "Boss",
"callSign": "5VDV2",
"flag": "TG",
"yearBuilt": null
}
]
]
代码:
describe('example to-do app', () => {
before(() => {
cy.visit('https://example.cypress.io/todo')
})
it('sets the local storage value from fixture', () => {
cy.fixture('requiredJsonData.json').then((value) => {
cy.log(value);
localStorage.setItem('entries', value);
cy.log(localStorage.getItem('entries'));
})
})
})
结果:
显然,我尝试将其读取并转换为地图,但在执行相同操作时会引发另一个错误“Iterator value null is not an entry object”。
我对地图的尝试:
it('gets the local storage value', () => {
const details = new Map([localStorage.getItem('entries')]);
// const details = new Map(localStorage.getItem('entries'));
var detailsSpecific = details.get('1000001');
cy.log(detailsSpecific);
})
尝试使用“地图”时出错:
一个(肮脏的)解决方案是,在继续之前从夹具中移除位置 1 和位置 n 的 '[' 和 ']' ,它会起作用,但我不想让它变得那么血腥。
有人可以指出我遗漏了什么并帮助您了解我如何解决它吗?
解决方案
在丹从评论中暗示之后,它奏效了。
更新代码:
it('sets the local storage value from fixture', () => {
cy.fixture('requiredJsonData.json').then((value) => {
cy.log(JSON.stringify(value));
localStorage.setItem('entries', JSON.stringify(value));
cy.log(localStorage.getItem('entries'));
const details = localStorage.getItem('entries');
cy.log(details);
})
})
推荐阅读
- graphql - GraphQL:一个接口可以用来统一两个等价属性名称不同的类型吗?
- css - 如何在导航栏中将按钮浮动到右侧,以免影响主导航栏?
- angular - 尝试使用对象属性访问文档信息
- javascript - componentDidMount() 中的“这是未定义”错误
- mysql - 从文本列中选择小时
- swift - 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[UIImageView _isResizable]:
- dart - 如何覆盖子类中的类变量?
- c# - Unity HoloLens 应用程序作为 Windows 应用程序而不是 AR 执行
- python - Spyder 和 Jupyter:与错误“TypeError:'list' 对象不能被解释为整数”的区别
- node.js - 如何使用 ngFor 从 Angular 中的数组对象中访问键值