2017-04-26 68 views
0

任何人都可以解釋這裏發生了什麼?我有這個簡單的類與一些靜態方法,我想測試它們。Ruby:奇怪的字符串比較斷言行爲

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 

也許這有助於實現我用帽子測試工具。

任何人都可以看到錯誤或解釋發生了什麼?

乾杯

+0

縮進故障是粘貼到SO編輯器中。他們很好IRL。 – Mats

回答

2

檢查被比較值的類型。其中之一不是一個字符串。 (因此,它不能等於一個字符串)。

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

<打我的頭>當然。萬分感謝。 – Mats