rspec的 - 为什么会发生这种属性比较USNG assert_equal时,它们是相同的只有失败

2019-08-01 06:16发布

两个测试失败 - 但只有在Ubuntu(12),但不是我的(新)的Mac。

失败两者的Active Record对象属性的比较,但在我所有的努力对他们进行比较,例如,使哈希公司在命令行和粘贴的属性,比较说,他们是相同的。

一个真正的益智游戏,因为我们有一个广泛的测试套件与许多数百次试验。 Rails的3.2.8,rspec的2.11

Failure/Error: assert_equal @iep_service.attributes, IepService.first.attributes
 MiniTest::Assertion:
   <{"id"=>414,
    "duration"=>30,
    "frequency"=>3,
    "period"=>"week",
    "group_size"=>"group",
    "location"=>nil,
    "service"=>nil,
    "area_of_need"=>"speech",
    "created_at"=>Tue, 09 Oct 2012 01:53:39 UTC +00:00,
    "updated_at"=>Tue, 09 Oct 2012 01:53:39 UTC +00:00,
    "therapist_id"=>nil,
    "start_date"=>nil,
    "end_date"=>nil,
    "student_id"=>469,
    "adhoc"=>false}> expected but was
   <{"id"=>414,
    "duration"=>30,
    "frequency"=>3,
    "period"=>"week",
    "group_size"=>"group",
    "location"=>nil,
    "service"=>nil,
    "area_of_need"=>"speech",
    "created_at"=>Tue, 09 Oct 2012 01:53:39 UTC +00:00,
    "updated_at"=>Tue, 09 Oct 2012 01:53:39 UTC +00:00,
    "therapist_id"=>nil,
    "start_date"=>nil,
    "end_date"=>nil,
    "student_id"=>469,
    "adhoc"=>false}>.
# (eval):2:in `assert_equal'

测试代码:

  context "And a pre-existing Iep Service for one of those students" do
    before(:each) { @iep_service = FactoryGirl.create(:iep_service, :student => @district.students.first) }

    context "And an Iep Service CSV" do
      before(:each) { @spreadsheet = IepServiceSpreadsheet.new(@district, open_spec_fixture_file('sample-ieps.csv')) }

      specify "Prevent importing" do
        # Leave database untouched
        assert_equal 1, IepService.count
        assert_equal @iep_service.attributes, IepService.first.attributes

        # Provide error report
        assert @spreadsheet.error_report.any?
      end 
    end 
  end 

我也试过: @iep_service.attributes.should == IepService.first.attributes

但得到:

 Failure/Error: @iep_service.attributes.should == IepService.first.attributes
   expected: {"id"=>421, "duration"=>30, "frequency"=>3, "period"=>"week", "group_size"=>"group", "location"=>nil, "service"=>nil, "area_of_need"=>"speech", "created_at"=>Tue, 09 Oct 2012 02:13:51 UTC +00:00, "updated_at"=>Tue, 09 Oct 2012 02:13:51 UTC +00:00, "therapist_id"=>nil, "start_date"=>nil, "end_date"=>nil, "student_id"=>483, "adhoc"=>false}
        got: {"id"=>421, "duration"=>30, "frequency"=>3, "period"=>"week", "group_size"=>"group", "location"=>nil, "service"=>nil, "area_of_need"=>"speech", "created_at"=>Tue, 09 Oct 2012 02:13:51 UTC +00:00, "updated_at"=>Tue, 09 Oct 2012 02:13:51 UTC +00:00, "therapist_id"=>nil, "start_date"=>nil, "end_date"=>nil, "student_id"=>483, "adhoc"=>false} (using ==)

用大差异,包括“检查“的实施:

      Diff:{"id"=>421, "duration"=>30, "frequency"=>3, "period"=>"week",  
"group_size"=>"group", "location"=>nil, "service"=>nil, "area_of_need"=>"speech",  
"created_at"=>Tue, 09 Oct 2012 02:13:51 UTC +00:00, "updated_at"=>Tue, 09 Oct 2012 02:13:51 
UTC +00:00, "therapist_id"=>nil, "start_date"=>nil, "end_date"=>nil, "student_id"=>483, 
"adhoc"=>false}.==({"id"=>421, "duration"=>30, "frequency"=>3, "period"=>"week", 
"group_size"=>"group", "location"=>nil, "service"=>nil, "area_of_need"=>"speech", "created_at"=>Tue, 09 Oct 2012 02:13:51 UTC +00:00, "updated_at"=>Tue, 09 Oct 2012 02:13:51 UTC +00:00, "therapist_id"=>nil, "start_date"=>nil, "end_date"=>nil, "student_id"=>483, "adhoc"=>false}) returned false even though the diff between {"id"=>421, "duration"=>30, "frequency"=>3, "period"=>"week", "group_size"=>"group", "location"=>nil, "service"=>nil, 
"area_of_need"=>"speech", "created_at"=>Tue, 09 Oct 2012 02:13:51 UTC +00:00, 
"updated_at"=>Tue, 09 Oct 2012 02:13:51 UTC +00:00, "therapist_id"=>nil, "start_date"=>nil, 
"end_date"=>nil, "student_id"=>483, "adhoc"=>false} and {"id"=>421, "duration"=>30, 
"frequency"=>3, "period"=>"week", "group_size"=>"group", "location"=>nil, "service"=>nil, 
"area_of_need"=>"speech", "created_at"=>Tue, 09 Oct 2012 02:13:51 UTC +00:00, 
"updated_at"=>Tue, 09 Oct 2012 02:13:51 UTC +00:00, "therapist_id"=>nil, "start_date"=>nil, 
"end_date"=>nil, "student_id"=>483, "adhoc"=>false} is empty. 
Check the implementation of 
{"id"=>421, "duration"=>30, "frequency"=>3, "period"=>"week", "group_size"=>"group", 
"location"=>nil, "service"=>nil, "area_of_need"=>"speech", "created_at"=>Tue, 09 Oct 2012 
02:13:51 UTC +00:00, "updated_at"=>Tue, 09 Oct 2012 02:13:51 UTC +00:00, 
"therapist_id"=>nil, "start_date"=>nil, "end_date"=>nil, "student_id"=>483, 
"adhoc"=>false}.==.

Answer 1:

没有真正的答案已经给出,所以我已经通过atrributes,如用了迭代

i=0
while i < @student.attributes.count
  assert_equal @student.attributes[i], Student.first.attributes[i]
  i+= 1
end

# Comparing field by field as ruby hash comparison isn't working right - 
# but only on Ubuntu!


文章来源: rspec - why does this attribute comparison usng assert_equal fail when they are the same, on ubuntu only?