0

次のメソッドは、テストに適切に合格します。

def associate_grid_location
        devices.each do |device|
            if device.grid_location and grid_location_id.nil?
                update_attribute(:grid_location_name,device.grid_location.grid_name)
                update_attribute(:grid_location_id,device.grid_location_id)
                break
            end
        end
    end

ただし、このメソッドの呼び出しで同じテスト ロジックを実行すると、各キャビネットの grid_location_name と grid_location_id の更新に失敗します。

def self.associate_grid_locations
        Cabinet.all.each do |cabinet|
            cabinet.associate_grid_location
        end
    end

失敗する理由を教えてください。

合格テスト:

test "Cabinet associate grid location from device where device has grid location" do
    @cabinet = cabinets(:one)
    @cabinet.grid_location_id = nil
    @device = devices(:one)
    @grid_location = grid_locations(:one)
    @grid_location.grid_name = "test location"
    @device.grid_location = @grid_location
    @cabinet.devices << @device
    @cabinet.associate_grid_location

    assert_equal @grid_location.grid_name, @cabinet.grid_location_name, "Failed: Did not update cabinet grid location"
    assert_equal @device.grid_location.id, @cabinet.grid_location_id, "Failed: Did not update cabinet grid location"
  end

失敗したテスト:

test "Cabinet associate grid location from device where device has grid location (Called on Cabinet)" do
    @cabinet = cabinets(:one)
    @cabinet.grid_location_id = nil
    @device = devices(:one)
    @grid_location = grid_locations(:one)
    @grid_location.grid_name = "test location"
    @device.grid_location = @grid_location
    @cabinet.devices << @device
    Cabinet.associate_grid_locations

    assert_equal @grid_location.grid_name, @cabinet.grid_location_name, "Failed: Did not update cabinet grid location"
    assert_equal @device.grid_location.id, @cabinet.grid_location_id, "Failed: Did not update cabinet grid location"
  end

ありがとうございました。

4

1 に答える 1

0

Cabinet.associate_grid_locationsこれはCabinet.all、テストでインスタンス化したオブジェクトにリンクされていない新しいオブジェクトをインスタンス化します (identity-map が有効になっていない場合)。他にエラーがないと仮定すると、#reloadonを呼び出すとテストに合格するはずです。@cabinet

于 2013-05-14T14:43:08.073 に答える