2

我在 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 的比较。似乎nilJava 中的实例化与nilJRuby 中的 a 不同 有人知道解决此问题的方法吗?

4

2 回答 2

3

使用runtime.getNil()而不是在new RubyNil(runtime) 这里

于 2012-09-04T08:46:59.890 回答
0

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

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

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

于 2012-09-04T00:15:47.103 回答