如何在 JSON 中编译空值时忽略 RSpec::ExampleGroups::EmptyContentValidation 错误?
How to ignore RSpec::ExampleGroups::EmptyContentValidation error on compiling a null value in JSON?
这是我得到的:
*** NameError Exception: undefined local variable or method `null' for #<RSpec::ExampleGroups::EmptyContentValidation:0x00007fdd2d2775d8>
这是我的代码:
actual =
{
"posts": [
{
"id": 3,
"title": "Post 3"
}
],
"profile": {
"name": ""
},
"nulldata": {
"res": null
}
}
- 不确定为什么
RSpec::ExampleGroups::EmptyContentValidation
编译上述 json 片段时抛出
- 如何摆脱这个错误信息(或者关闭这个不相关的错误)
那不是 JSON,那是 Ruby 哈希。在 Ruby 中没有 null
,而是使用 nil
。您可以通过粘贴到您的 REPL 来确认所有这些:
actual =
{
"posts": [
{
"id": 3,
"title": "Post 3"
}
],
"profile": {
"name": ""
},
"nulldata": {
"res": null
}
}
这将 return:
NameError: undefined local variable or method `null' for main:Object
现在改为nil
:
actual =
{
"posts": [
{
"id": 3,
"title": "Post 3"
}
],
"profile": {
"name": ""
},
"nulldata": {
"res": nil
}
}
这将 return:
=> {
:posts => [
[0] {
:id => 3,
:title => "Post 3"
}
],
:profile => {
:name => ""
},
:nulldata => {
:res => nil
}
}
而且您甚至可以确认它不是 JSON(String
)而是 Hash
:
actual.class
=> Hash < Object
这是我得到的:
*** NameError Exception: undefined local variable or method `null' for #<RSpec::ExampleGroups::EmptyContentValidation:0x00007fdd2d2775d8>
这是我的代码:
actual =
{
"posts": [
{
"id": 3,
"title": "Post 3"
}
],
"profile": {
"name": ""
},
"nulldata": {
"res": null
}
}
- 不确定为什么
RSpec::ExampleGroups::EmptyContentValidation
编译上述 json 片段时抛出 - 如何摆脱这个错误信息(或者关闭这个不相关的错误)
那不是 JSON,那是 Ruby 哈希。在 Ruby 中没有 null
,而是使用 nil
。您可以通过粘贴到您的 REPL 来确认所有这些:
actual =
{
"posts": [
{
"id": 3,
"title": "Post 3"
}
],
"profile": {
"name": ""
},
"nulldata": {
"res": null
}
}
这将 return:
NameError: undefined local variable or method `null' for main:Object
现在改为nil
:
actual =
{
"posts": [
{
"id": 3,
"title": "Post 3"
}
],
"profile": {
"name": ""
},
"nulldata": {
"res": nil
}
}
这将 return:
=> {
:posts => [
[0] {
:id => 3,
:title => "Post 3"
}
],
:profile => {
:name => ""
},
:nulldata => {
:res => nil
}
}
而且您甚至可以确认它不是 JSON(String
)而是 Hash
:
actual.class
=> Hash < Object