Ruby 如何在JSON中编译空值时忽略RSpec::ExampleGroups::EmptyContentValidation错误?

Ruby 如何在JSON中编译空值时忽略RSpec::ExampleGroups::EmptyContentValidation错误?,ruby,rspec,Ruby,Rspec,以下是我得到的: *** NameError Exception: undefined local variable or method `null' for #<RSpec::ExampleGroups::EmptyContentValidation:0x00007fdd2d2775d8> 不确定为什么RSpec::ExampleGroups::EmptyContentValidation是 编译上述json代码段时引发 如何消除此错误消息(或关闭此不相关的错误) 这不是JS

以下是我得到的:

*** NameError Exception: undefined local variable or method `null' for #<RSpec::ExampleGroups::EmptyContentValidation:0x00007fdd2d2775d8>

  • 不确定为什么
    RSpec::ExampleGroups::EmptyContentValidation
    是 编译上述json代码段时引发
  • 如何消除此错误消息(或关闭此不相关的错误)

  • 这不是JSON,而是Ruby哈希。在Ruby中没有
    null
    ,而是使用
    nil
    。您可以通过粘贴到REPL中来确认所有这些:

    actual =
            {
                "posts": [
                    {
                        "id": 3,
                        "title": "Post 3"
                    }
                ],
                "profile": {
                    "name": ""
                },
                "nulldata": {
                    "res": null
                }
            }
    
    这将返回:

    NameError: undefined local variable or method `null' for main:Object
    
    => {
           :posts => [
            [0] {
                   :id => 3,
                :title => "Post 3"
            }
        ],
         :profile => {
            :name => ""
        },
        :nulldata => {
            :res => nil
        }
    }
    
    现在将其更改为
    nil

    actual =
            {
                "posts": [
                    {
                        "id": 3,
                        "title": "Post 3"
                    }
                ],
                "profile": {
                    "name": ""
                },
                "nulldata": {
                    "res": nil
                }
            }
    
    这将返回:

    NameError: undefined local variable or method `null' for main:Object
    
    => {
           :posts => [
            [0] {
                   :id => 3,
                :title => "Post 3"
            }
        ],
         :profile => {
            :name => ""
        },
        :nulldata => {
            :res => nil
        }
    }
    
    您甚至可以确认它不是JSON(它是一个
    字符串
    ),而是一个
    散列

    actual.class
    => Hash < Object
    
    actual.class
    =>散列<对象
    
    这不是JSON,而是Ruby哈希。在Ruby中没有
    null
    ,而是使用
    nil
    。您可以通过粘贴到REPL中来确认所有这些:

    actual =
            {
                "posts": [
                    {
                        "id": 3,
                        "title": "Post 3"
                    }
                ],
                "profile": {
                    "name": ""
                },
                "nulldata": {
                    "res": null
                }
            }
    
    这将返回:

    NameError: undefined local variable or method `null' for main:Object
    
    => {
           :posts => [
            [0] {
                   :id => 3,
                :title => "Post 3"
            }
        ],
         :profile => {
            :name => ""
        },
        :nulldata => {
            :res => nil
        }
    }
    
    现在将其更改为
    nil

    actual =
            {
                "posts": [
                    {
                        "id": 3,
                        "title": "Post 3"
                    }
                ],
                "profile": {
                    "name": ""
                },
                "nulldata": {
                    "res": nil
                }
            }
    
    这将返回:

    NameError: undefined local variable or method `null' for main:Object
    
    => {
           :posts => [
            [0] {
                   :id => 3,
                :title => "Post 3"
            }
        ],
         :profile => {
            :name => ""
        },
        :nulldata => {
            :res => nil
        }
    }
    
    您甚至可以确认它不是JSON(它是一个
    字符串
    ),而是一个
    散列

    actual.class
    => Hash < Object
    
    actual.class
    =>散列<对象