【问题标题】:Assert that an array is ordered with minitest断言数组是用 minitest 排序的
【发布时间】:2016-02-06 19:55:27
【问题描述】:

我需要能够使用 ruby​​ 编写一个带有 minitest 的测试,断言给定数组是按 asc 或 desc 排序的。

我有不完整的代码如下:

 it "can order an array by asc order" do
     assert_equal tested_array, sorted_array
end

【问题讨论】:

  • 你的具体问题是什么,你能提供一个代码示例吗?
  • 欢迎来到 Stack Overflow。我们希望看到您的尝试。 SO 不是“给我代码”网站,而是“帮我修复我的代码”,您的努力非常重要。请阅读“How to Ask”和“minimal reproducible example”。
  • @theTinMan 我编辑了这个问题。谢谢

标签: ruby minitest


【解决方案1】:

你可以这样写:

test "array should be sorted asc" do
  sorted_array = tested_array.sort
  assert_equal tested_array, sorted_array, "Array sorted"
end

desc 相同,但写成 tested_array.sort.reverse 而不是 tested_array.sort

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-27
    • 2020-12-17
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 2020-12-04
    相关资源
    最近更新 更多