使用Ruby测试驱动开发

时间:2015-01-17 17:59:14

标签: ruby unit-testing testing tdd

我是测试的新手,需要一点帮助才能开始使用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 

2 个答案:

答案 0 :(得分:1)

你以正确的方式做所有事情。

您需要修复一些次要语法项。

当您需要测试文件时,您通常只需要minitest/autorun,而不是test/unit。 (Minitest是典型的较新的Ruby版本,测试/单元是较旧的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)

首先,许多现有的测试教程都显示了较旧的语法。如果您尝试在较新的系统上运行较旧的语法,您可能会看到警告和错误,例如&#34; TestCase现在是Test&#34;或者&#34; MiniTest现在是Minitest&#34;。

答案 1 :(得分:0)

这是一个非常简单的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)来测试您的文件阅读/转换练习。