1

可能重复:
rspec - 为什么这个属性比较使用 assert_equal 当它们相同时会失败,仅在 ubuntu 上?

Ruby: 1.9.3-p194  
Rails: 3.2.8  
Ubuntu: 12.04

测试有很多设置,然后最终会:

assert_equal @iep_service.attributes, IepService.first.attributes

在 mac 上工作,但在 ubuntu 上失败:

  2) Iep Service Spreadsheet A typical district With pre-existing students And a pre-existing Iep Service for one of those students And an Iep S[52/427$
SV Prevent importing
     Failure/Error: assert_equal @iep_service.attributes, IepService.first.attributes
     MiniTest::Assertion:
       <{"id"=>212,
        "duration"=>30,
        "frequency"=>3,
        "period"=>"week",
        "group_size"=>"group",
        "location"=>nil,
        "service"=>nil,
        "area_of_need"=>"speech",
        "created_at"=>Wed, 24 Oct 2012 18:59:47 UTC +00:00,
        "updated_at"=>Wed, 24 Oct 2012 18:59:47 UTC +00:00,
        "therapist_id"=>nil,
        "start_date"=>nil,
        "end_date"=>nil,
        "student_id"=>233,
        "adhoc"=>false}> expected but was
       <{"id"=>212,
        "duration"=>30,
        "frequency"=>3,
        "period"=>"week",
        "group_size"=>"group",
        "location"=>nil,
        "service"=>nil,
        "area_of_need"=>"speech",
        "created_at"=>Wed, 24 Oct 2012 18:59:47 UTC +00:00,
        "updated_at"=>Wed, 24 Oct 2012 18:59:47 UTC +00:00,
        "therapist_id"=>nil,
        "start_date"=>nil,
        "end_date"=>nil,
        "student_id"=>233,
        "adhoc"=>false}>.
     # (eval):2:in `assert_equal'
     # ./spec/models/iep_service_spreadsheet_spec.rb:71:in `block (6 levels) in <top (required)>'

如果有帮助,完整的来源是:

context "A typical district" do
  before(:each) { set_up_district }

  context "With pre-existing students" do
    before(:each) { StudentSpreadsheet.new(@district, open_spec_fixture_file('sample-students.csv')) }

    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.c    sv')) }
        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
  end
end
4

1 回答 1

0

assert_equal uses the operator/method ==.

You can read the documentation for assert_equal here: http://ruby-doc.org/core/classes/Test/Unit/Assertions.html#M006665. It comes directly from Ruby, Rails don't overrides the definition.

So depending on the object type the == bahaves differently, and also as it comes from Ruby there may be a different implementation or slightly difference in the ruby code.

Anyways, it is better for you to compare value by value or live with the == comparison may bring to you.

于 2012-10-24T21:04:43.730 回答