【问题标题】:Test Driven Development with Ruby使用 Ruby 进行测试驱动开发
【发布时间】:2015-01-17 17:59:14
【问题描述】:

我是测试新手,在开始使用 TDD 时需要一些帮助。我有一个简单的应用程序,它需要一些 txt 文件并为不同的输出重新格式化它们。

这里是一个txt文件的例子

Smith | Steve | D | M | Red | 3-3-1985
Bonk | Radek | S | M | Green | 6-3-1978
Bouillon | Francis | G | M | Blue | 6-3-1975 

这是我在 app.rb 中更改此文本文件输出的方法

def pipe
 alpha = File.readlines('pipe.txt').sort 
 alpha.each {|line| line.gsub! '-', '/'}
 alpha.each {|line| line.gsub! '|', ''}
 alpha.each {|line| line.gsub! 'M', 'Male'}
end

def pipe_date
 alpha = File.readlines('pipe.txt')
 alpha.each {|line| line.gsub! '-', '/'}
 alpha.each {|line| line.gsub! '|', ''}
 alpha.each {|line| line.gsub! 'M', 'Male'}
 alpha.sort_by { |str| Date.strptime(str[/\d+\/\d+\/\d+/], "%d/%m/%Y") } 
end

def pipe_des
  alpha = File.readlines('pipe.txt').sort { |a,b| b <=> a }
  alpha.each {|line| line.gsub! '-', '/'}
  alpha.each {|line| line.gsub! '|', ''}
  alpha.each {|line| line.gsub! 'M', 'Male'}
end

看了一圈之后,我写了一个看起来像这样的test.rb文件,但是当我运行ruby test.rb时,我得到了这个错误

MiniTest::Unit::TestCase is now Minitest::Test. From        /Users/pacloan/.rbenv/versions/2.1.2/lib/ruby/2.1.0/test/unit/testcase.rb:8:in   `<module:Unit>'
/Users/pacloan/.rbenv/versions/2.1.2/lib/ruby/2.1.0/test/unit.rb:676:in   `<class:Runner>': undefined method `_run_suite' for class `Test::Unit::Runner'   (NameError)

这是我的 test.rb 文件。我认为我的设置可能是错误的。有人可以提供一些关于我在做什么的见解吗?

require_relative "app"
require 'minitest'
require 'test/unit'
require 'minitest/autorun'
Minitest::Test

class TestApp < Test::Unit::TestCase

  def test_read_files
    #assert something
    #expected output 
  end
end 

【问题讨论】:

  • “它不起作用”是什么意思?你采取了什么行动?你期待什么结果?你观察到了什么结果?
  • 我对其进行了编辑以显示我在做什么,我运行了 test.rb 并得到了一个错误,我对测试非常陌生,不知道如何真正开始并正确运行它们
  • 你可以看看这个 - mattsears.com/articles/2011/12/10/minitest-quick-reference .. 这个教程会给你一个好的开始.. 为什么不是 Rspec?只是问它很容易使用.. AFAIK..
  • 如果你愿意,你也可以看到this answer。你可以看到如何运行和编写它。

标签: ruby unit-testing testing tdd


【解决方案1】:

你所做的一切都是正确的。

您需要修复一些小的语法项目。

当您需要测试文件时,通常只需要minitest/autorun,而不需要test/unit。 (Minitest 是较新的 Ruby 版本的典型,test/unit 是较旧的 Ruby 版本的典型)。

您可以删除这些行:

require 'minitest'
require 'test/unit'

还有这一行:

Minitest::Test

改变这个:

class TestApp < Test::Unit::TestCase

到这里:

class TestApp < Minitest::Test

这是新语法。

您可以像这样验证您的 Ruby 是最新的(版本 2.2.x):

$ ruby -v
ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-darwin14]

您可以像这样验证您的 minitest gem 是最新的(版本 5.5.x):

$ gem list minitest
minitest (5.5.1)

请注意,许多现有的测试教程都显示了较旧的语法。如果您尝试在较新的系统上运行旧语法,您可能会看到警告和错误,例如“TestCase is now Test”或“MiniTest is now Minitest”。

【讨论】:

    【解决方案2】:

    下面是一个非常简单的 TDD 练习:

    假设我们需要一个将日期字符串作为输入并将该字符串更改为使用“/”而不是“-”的方法。

    即给定3-3-1985,该方法应输出3/3/1985

    让我们把它写成first.rb文件中的测试:

    # first.rb
    require 'test/unit' 
    
    class TestApp < Test::Unit::TestCase
      test 'method to replace - with / in dates' do
        assert_equal "3/3/1985", format_date("3-3-1985")
      end 
    end
    

    使用ruby first.rb 运行它;它将给出以下输出:

    Loaded suite first
    Started
    E
    ====================================================================================================================================
    Error: test: method to replace - with / in dates(TestApp)
    : NoMethodError: undefined method `format_date' for #    <TestApp:0x007ffc522493b0>
    first.rb:5:in `block in <class:TestApp>'
    ====================================================================================================================================
    
    
    Finished in 0.00177 seconds.
    -----------------------------------------------------------------------    -------------------------------------------------------------
    1 tests, 0 assertions, 0 failures, 1 errors, 0 pendings, 0 omissions, 0     notifications
    0% passed
    ------------------------------------------------------------------------------------------------------------------------------------
    

    正如它所说,没有format_date 方法。首先,将方法添加到同一文件中,如下所示:

    # first.rb
    require 'test/unit' 
    
    class TestApp < Test::Unit::TestCase
      test 'method to replace - with / in dates' do
        assert_equal "3/3/1985", format_date("3-3-1985")
      end 
    end
    
    def format_date(given_date)
    end
    

    使用ruby first.rb再次运行测试

    现在错误将是:

    ====================================================================================================================================
    Failure: test: method to replace - with / in dates(TestApp)
    first.rb:5:in `block in <class:TestApp>'
         2: 
         3: class TestApp < Test::Unit::TestCase
         4:   test 'method to replace - with / in dates' do
      => 5:     assert_equal "3/3/1985", format_date("3-3-1985")
         6:   end
         7: end
         8: 
    <"3/3/1985"> expected but was
    <nil>
    ====================================================================================================================================
    

    所以可以调用该方法,但它返回的是nil,而不是预期的3/3/1985。修改方法如下:

    def format_date(given_date)
      given_date.gsub! '-', '/'
    end
    

    现在当你运行测试时,输出将是:

    Loaded suite first
    Started
    .
    
    Finished in 0.00068 seconds.
    ------------------------------------------------------------------------------------------------------------------------------------
    1 tests, 1 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
    100% passed
    ------------------------------------------------------------------------------------------------------------------------------------
    

    就是这样。我们从一个失败的小测试开始,一直努力通过测试,实现了所需的功能。

    遵循相同的原则 (write a small failing test / make it pass / repeat) 来测试您的文件读取/转换练习。

    【讨论】:

    • 啊!不错的 TDD 示例.. 简单但甜蜜 :)
    • 这是一个很好的教程。它没有回答OP的问题。他的错误信息显示他只是有语法故障。
    • @joelparkerhenderson 他想要“一些关于 TDD 入门的帮助”。
    • @PrakashMurthy 你在那里提到了你用来编写这个测试的 Ruby 版本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-23
    • 1970-01-01
    • 2015-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-19
    相关资源
    最近更新 更多