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

时间:2012-09-03 23:14:43

标签: rspec jruby

我在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

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

2 个答案:

答案 0 :(得分:3)

使用runtime.getNil()代替new RubyNil(runtime) here

答案 1 :(得分:0)

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

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

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