Ruby:奇怪的字符串比较断言行为

时间:2017-04-26 08:52:12

标签: ruby string unit-testing

有人能解释这里发生了什么吗?我有一些带有静态方法的简单类,我想测试它们。

yaqueline/build/converters/asciidocconverter.rb

# encoding: UTF-8
require 'asciidoctor'

module Yaqueline
  module Build
    module Converters

      class AsciiDocConverter < Converter

        class << self

          def matches path
            path =~ /\.(asciidoc|adoc|ascii|ad)$/
          end

          def convert content
             html = Asciidoctor.convert content, to_file: false, safe: :safe
              html = get_guts_out_of_body html
              puts "asciidoc #{html}"
              html
            end

            def get_guts_out_of_body html
              if html =~ /<body>/
                puts "get guts: #{html}"
                return html.match(%r{(?<=<body>).*(?=</body>)})
              end
              html
            end

          end # class << self

        end # class

      end
    end
  end

test/build/converters/asciidocconverter_test.rb中的测试:

# encoding: utf-8
require 'helper'
require 'yaqueline/build/converters/asciidocconverter'

class TestAsciidocConverter < Test::Unit::TestCase

  should "be able to get body html from a document" do
value = %q{SUCCESS}
html = %Q{
     <html>
       <head>
     <title>Hej värld</title>
       </head>
       <body>#{value}</body>
     </html>}
guts = Yaqueline::Build::Converters::AsciiDocConverter.get_guts_out_of_body html
puts "guts was '#{guts}'"
assert value.eql?(guts), "guts was '#{guts}', expected '#{value}'"
  end

end

使用

运行测试时
$ rake test TEST=test/build/converters/asciidocconverter_test.rb

结果对我来说很好看:

Started
get guts: 
     <html>
       <head>
     <title>Hej värld</title>
       </head>
       <body>SUCCESS</body>
     </html>
guts was 'SUCCESS'
F
===============================================================================================================================================================================
Failure:
  guts was 'SUCCESS', expected 'SUCCESS'.
  <false> is not true.
test: AsciidocConverter should be able to get body html from a document. (TestAsciidocConverter)
/Users/mats/src/examples/yaqueline/test/build/converters/asciidocconverter_test.rb:37:in `block in <class:TestAsciidocConverter>'
/Users/mats/src/examples/yaqueline/test/build/converters/asciidocconverter_test.rb:39:in `instance_exec'
/Users/mats/src/examples/yaqueline/test/build/converters/asciidocconverter_test.rb:39:in `block in create_test_from_should_hash'
===============================================================================================================================================================================

但断言失败,这对我来说很奇怪,我需要一些帮助。

我正在运行ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin15] 而我的Gemfile看起来像

# Add dependencies required to use your gem here.
# Example:
#   gem "activesupport", ">= 2.3.5"

gem 'mercenary'
gem 'safe_yaml'
gem 'kramdown'
gem 'colorator'
gem 'pathutil'
gem 'nokogiri'
gem 'sass'
gem 'listen', '~> 3.0'
gem 'asciidoctor'
gem 'tilt'
gem 'erubis'

# Add dependencies to develop your gem here.
# Include everything needed to run rake, tests, features, etc.
group :development do
  gem "rdoc", "~> 3.12"
  gem "bundler", "~> 1.0"
  gem "juwelier", "~> 2.1.0"
  gem "simplecov", ">= 0"
  gem 'rubocop', '~> 0.48.1', require: false
  gem 'thin' # or whatever I end up with
  gem 'minitest'
  gem 'test-unit'
  gem 'shoulda'
end

也许这有助于实现我正在使用的帽子测试工具。

任何人都可以看到错误或解释发生了什么事吗?

干杯

1 个答案:

答案 0 :(得分:2)

检查要比较的值的类型。其中一个不是字符串。 (因此,它不能等于字符串)。

 guts = html.match(%r{(?<=<body>).*(?=</body>)})
 guts # => #<MatchData "SUCCESS">
 guts.to_s # => "SUCCESS"