JRuby:nil!=nil:RSpec测试失败,即使数组相同



我在JRuby中看到一个问题,即使预期和实际输出相同,我的rspec规范也会失败。这是故障消息:

Failures:
  1) PgArrayParser#parse_pg_array one dimensional arrays NULL values returns an array of strings, with nils replacing NULL characters
     Failure/Error: parser.parse_pg_array(%[{1,NULL,NULL}]).should eq ['1',nil,nil]
       expected: ["1", nil, nil]
            got: ["1", nil, nil]
       (compared using ==)
       Diff:["1", nil, nil].==(["1", nil, nil]) returned false even though the diff between ["1", nil, nil] and ["1", nil, nil] is empty. Check the implementation of ["1", nil, nil].==.
     # ./spec/parser_spec.rb:26:in `(root)'
  2) PgArrayParser#parse_pg_array two dimensional arrays strings returns an array of strings with a sub array and a quoted }
     Failure/Error: parser.parse_pg_array(%[{1,{"2,}3",NULL},4}]).should eq ['1',['2,}3',nil],'4']
       expected: ["1", ["2,}3", nil], "4"]
            got: ["1", ["2,}3", nil], "4"]
       (compared using ==)
       Diff:["1", ["2,}3", nil], "4"].==(["1", ["2,}3", nil], "4"]) returned false even though the diff between ["1", ["2,}3", nil], "4"] and ["1", ["2,}3", nil], "4"] is empty. Check the implementation of ["1", ["2,}3", nil], "4"].==.
     # ./spec/parser_spec.rb:69:in `(root)'
  3) PgArrayParser#parse_pg_array three dimensional arrays returns an array of strings with sub arrays
     Failure/Error: parser.parse_pg_array(%[{1,{2,{3,4}},{NULL,6},7}]).should eq ['1',['2',['3','4']],[nil,'6'],'7']
       expected: ["1", ["2", ["3", "4"]], [nil, "6"], "7"]
            got: ["1", ["2", ["3", "4"]], [nil, "6"], "7"]
       (compared using ==)
       Diff:["1", ["2", ["3", "4"]], [nil, "6"], "7"].==(["1", ["2", ["3", "4"]], [nil, "6"], "7"]) returned false even though the diff between ["1", ["2", ["3", "4"]], [nil, "6"], "7"] and ["1", ["2", ["3", "4"]], [nil, "6"], "7"] is empty. Check the implementation of ["
1", ["2", ["3", "4"]], [nil, "6"], "7"].==.
     # ./spec/parser_spec.rb:87:in `(root)'
Finished in 0.2 seconds
15 examples, 3 failures

使用=~而不是eq==产生以下输出:

..F.......F..F.
Failures:
  1) PgArrayParser#parse_pg_array one dimensional arrays NULL values returns an array of strings, with nils replacing NULL characters
     Failure/Error: parser.parse_pg_array(%[{1,NULL,NULL}]).should =~ ['1',nil,nil]
       expected collection contained:  ["1", nil, nil]
       actual collection contained:    ["1", nil, nil]
       the missing elements were:      [nil, nil]
       the extra elements were:        [nil, nil]
     # ./spec/parser_spec.rb:26:in `(root)'
  2) PgArrayParser#parse_pg_array two dimensional arrays strings returns an array of strings with a sub array and a quoted }
     Failure/Error: parser.parse_pg_array(%[{1,{"2,}3",NULL},4}]).should =~ ['1',['2,}3',nil],'4']
       expected collection contained:  ["1", ["2,}3", nil], "4"]
       actual collection contained:    ["1", ["2,}3", nil], "4"]
       the missing elements were:      [["2,}3", nil]]
       the extra elements were:        [["2,}3", nil]]
     # ./spec/parser_spec.rb:69:in `(root)'
  3) PgArrayParser#parse_pg_array three dimensional arrays returns an array of strings with sub arrays
     Failure/Error: parser.parse_pg_array(%[{1,{2,{3,4}},{NULL,6},7}]).should =~ ['1',['2',['3','4']],[nil,'6'],'7']
       expected collection contained:  ["1", ["2", ["3", "4"]], [nil, "6"], "7"]
       actual collection contained:    ["1", ["2", ["3", "4"]], [nil, "6"], "7"]
       the missing elements were:      [[nil, "6"]]
       the extra elements were:        [[nil, "6"]]
     # ./spec/parser_spec.rb:87:in `(root)'
Finished in 0.17 seconds
15 examples, 3 failures

规格在这里。需要注意的是,这些比较是唯一包含nils的比较。在Java中实例化的nil似乎与JRuby中的nil不同。有人知道解决这个问题的方法吗?

此处使用runtime.getNil()而不是new RubyNil(runtime)

是否尝试使用=~匹配数组?例如:

[1,2,3].should =~ [1,2,3]

几天前,我在匹配数组时遇到了问题,这对我来说很好。

相关内容

  • 没有找到相关文章

最新更新