Ruby:Minitest / spec和BDD Gherkin

时间:2011-10-05 05:08:23

标签: ruby bdd minitest

似乎我找不到很多关于Minitest / spec的文档,所以我想知道是否有人可以帮我弄清楚如何做我需要做的事情。基本上我想在我的所有类和模块上运行测试以确保它们1.)输出正确的值类型,如果是静态或2.)在to_symbols的情况下,所有键都是符号。这是我到目前为止所尝试的:

我认为小黄瓜会是这样的:

Given binns
When the version method is called
then the return should be a float
and version should be a method or constant

我认为这将是minitest / spec:

require 'minitest/autorun'
require 'minitest/spec'
require 'binns'

given Binns do
  when "the version method is called" do
    then "the return should be a float" do
      # Do work
    end
  end
end

但我明白了:

syntax error, unexpected keyword_when (SyntaxError)
  when "the version method is called" do
      ^

注意:我也对其他测试建议持开放态度(我对黄瓜知之甚少,听说它很大)或者如果有人有书的建议,请告诉我一直在寻找一本好书Ruby单元测试。

1 个答案:

答案 0 :(得分:2)

MiniTest / Spec使用Rspec风格的语法:desc,it,before,after ...

require 'minitest/autorun'
require 'minitest/spec'
require 'binns'

desc Binns do
  it "should return a float when the version method is called" do
    # Do work
  end
end

如果它让你更容易过渡,你可以将“desc”方法别名为“given”,将“it”方法别名为“when”。

Good tutorial from Peter Cooper here.