Javascript 每个名称访问对象键
是否可以访问Javascript 每个名称访问对象键,javascript,arrays,object,ecmascript-6,jestjs,Javascript,Arrays,Object,Ecmascript 6,Jestjs,是否可以访问的名称部分内的对象密钥。每个 let accounts = [ { details: { company_name: "company_name", email, password: "asdf", },
的名称
部分内的对象密钥。每个
let accounts =
[
{
details:
{
company_name:
"company_name",
email,
password:
"asdf",
},
find:
[
"_id",
"company_name",
"email",
"type",
],
type:
"creator"
},
{
details:
{
email,
first_name:
"first_name",
last_name:
"last_name",
password:
"asdf",
},
find:
[
"_id",
"email",
"first_name",
"last_name",
"type",
],
type:
"user"
},
]
describe.each(accounts)(
"%s", // <-- access the 'type' key, e.g. account.type
function (account)
{
// test code
}
)
let账户=
[
{
细节:
{
公司名称:
“公司名称”,
电子邮件,
密码:
“asdf”,
},
查找:
[
“_id”,
“公司名称”,
“电子邮件”,
“类型”,
],
类型:
“创造者”
},
{
细节:
{
电子邮件,
姓名:
“名字”,
姓氏:
“姓”,
密码:
“asdf”,
},
查找:
[
“_id”,
“电子邮件”,
“名字”,
“姓”,
“类型”,
],
类型:
“用户”
},
]
描述每个(账户)(
“%s”,//Jest需要在第一个参数中包含一个数组。如果传入1D数组,它将在内部映射到一个数组数组(即传入[1,2,3]
,因为第一个参数将转换为[[1],[2],[3]
)
数组中的每个数组都用作测试套件的数据。因此,在上一个示例中,描述。每个将生成三个测试套件,第一个以1
作为数据,第二个以2
作为数据,第三个以3
作为数据
现在,在测试套件名称中,您只能格式化提供给它的参数。在您的情况下,您将向每个测试套件传递accounts
数组的每个对象中的数据。因此,当您在测试套件名称中设置格式说明符时,它们将应用于整个account对象(即,示例中的%s
将字符串化对象,从而产生[对象对象]
)。不幸的是,我认为您无法将格式说明符应用于对象的键
实现您想要的目标的一些想法:
解决方案1
如果您使用%s
格式化程序组成测试套件名称,将调用对象的toString
方法(默认情况下返回[Object Object]
)
如果在每个accounts对象中定义toString
方法,则将使用该方法。因此,我们可以使用此代码将toString
方法添加到每个account对象中(请注意,我们正在添加的toString
方法返回类型
键的值):
现在,使用%s
格式说明符,您应该可以在每个测试套件中看到帐户类型:
describe.each(accounts)(
"%s", // <-- This will cause the toString method to be called.
function (account)
{
// test code
}
)
现在可以使用第一个参数(即帐户类型)为测试套件命名:
describe.each(accounts)(
'%s', // <-- This %s will format the first item in each test suite array.
function (accountType, account) {
// test code
}
);
此解决方案的缺点是,您必须手动将模板文本中的每个测试套件数据追加到新行(即,如果您将新元素添加到accounts
数组中,您必须记住将其作为${accounts[2]}
添加到新行的模板文本中).您可以映射初始帐户数组,将每个帐户转换为包含2个项目的数组:
帐户类型
初始帐户元素
现在,您可以使用description
name中的第一个元素数组
describe.each(accounts.map(account => [account.type, account]))(
'testing %s', // %s replaced by account type
(type, account) => { // note: 2 arguments now
it('details should be defined ', () => {
expect(account.details).toBeDefined();
});
},
);
我对一个对象也有类似的问题。我想根据http错误代码测试一条错误消息,所以我编写了一个测试对象,如下所示:
const expectedElements={
错误:{
代码:500,
标题:“阿维克拉皮问题”
},
未找到:{
代码:404,
标题:“Éelement缺席”
},
未经授权:{
代码:401,
标题:“Accès non-autorisé”
}
};
我使用Object.entries(obj)
得到一个数组,其中的条目是这样写的:['key','value']
。在测试中,我可以将它们作为两个参数来访问。我是这样写的:
test.each(Object.entries(expectedElement))(“NoAccess显示%s的正确元素”,(key,expectedElement)=>{
const{getByRole}=render();
//测试代码
});
现在我可以添加任意数量的用例,而不必重写测试或创建数组。我只需在我的expectedElements
对象中写入一个新值。另外,我还有一个描述性测试名称!你说的“对象的键”是什么意思?一个对象包含键。你是指索引吗?@JackBashford试图访问对象中的类型
。所以在第一次迭代中,它将是创建者
,第二次迭代,用户
。对吗?@JackBashford yessince描述。每个
都使用util.format
来生成名称,我看不到任何方法eving目标。util.format
访问特定属性如果使用快照匹配,则需要为每个测试创建唯一的标题,因为这将是快照文件的名称。我希望这是最清晰的答案!
describe.each(accounts)(
'%s', // <-- This %s will format the first item in each test suite array.
function (accountType, account) {
// test code
}
);
describe.each`
account
${accounts[0]}
${accounts[1]}
`('$account.type', function (account) {
// test code
});
describe.each(accounts.map(account => [account.type, account]))(
'testing %s', // %s replaced by account type
(type, account) => { // note: 2 arguments now
it('details should be defined ', () => {
expect(account.details).toBeDefined();
});
},
);