ruby - MiniTest 和非拉丁测试描述

标签 ruby testing unicode utf-8 minitest

Ruby 1.9 拥有很酷的 Unicode 支持,是吗?

# encoding: utf-8
require 'minitest/spec'
require 'minitest/autorun'

describe "test" do
  it "α β γ δ & a b c d" do
    (1+1).must_equal 3
  end
end

# 1) Failure:
# test_0001__a_b_c_d(TestSpec) [test.rb:7]:
# Expected 3, not 2.

我的非拉丁字母在哪里?我应该总是用糟糕的英语写测试吗?

因为我可以使用任何 Unicode 符号定义方法:

def α_β_γ_δ_a_b_c_d
  puts "geeeek"
end

α_β_γ_δ_a_b_c_d
#=> "geeeek"

PS我的问题似乎不清楚。我想问如何制作minitest的失败描述来显示我的非拉丁定义。

最佳答案

大约是regexp used here 。在使用 /\s+/ 进行猴子修补 /\W+/ 后,它显示 utf-8 字符。

# encoding: utf-8
require 'minitest/spec'
require 'minitest/autorun'

class MiniTest::Spec < MiniTest::Unit::TestCase
  def self.it desc = "anonymous", &block
    block ||= proc { skip "(no tests defined)" }

    @specs ||= 0
    @specs += 1

    # regexp /\W+/ replaced with /\s+/
    name = "test_%04d_%s" % [ @specs, desc.gsub(/\s+/, '_').downcase ]

    define_method name, &block

    self.children.each do |mod|
      mod.send :undef_method, name if mod.public_method_defined? name
    end
  end
end

describe "test" do
  it "α β γ δ & a b c D" do
    (1+1).must_equal 3
  end
end

#   1) Failure:
# test_0001_α_β_γ_δ_&_a_b_c_d(test) [forwarding.rb:24]:
# Expected: 3
#   Actual: 2

关于ruby - MiniTest 和非拉丁测试描述,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9430930/

相关文章:

ruby-on-rails - puts 不打印东西到控制台

ruby-on-rails - 在 rspec 的 Controller 测试中使用 rails 的 "post",在路由上使用范围和协议(protocol)

ruby-on-rails - 在 ruby​​ 中用另一个数组中的字符串替换一个数组中的字符串

ruby-on-rails - 由于 ActiveRecord::StatementInvalid: SQLite3::SQLException: 无法回滚,Rails delayed_job 不起作用

laravel - 如何使用 PHPUnit 在 Laravel 上测试更复杂的案例

c# - CodedUI 测试更改 URL

delphi - Delphi 6 可以将 UTF-8 葡萄牙语转换为 WideString 吗?

selenium - 守夜人 : Basic auth with access_key not working

python - 更改用于 ‘print’ 参数类型强制的字符串类型

python - 将 CSV 文件中的 unicode 键/值存储到字典中时出现 UTF-8 "inconsistency"