簡體   English   中英

RSpec場景概述:多個測試用例

[英]RSpec Scenario Outlines: Multiple Test Cases

使用RSpec測試一堆不同測試用例的最佳方法是什么?

例如,給定string-additions.rb

require 'rspec'

class String
  if method_defined? :reverse_words
    raise "String#reverse_words is already defined"
  end
  def reverse_words
    split(' ').reverse!.join(' ')
  end
end

describe String do
  describe "#reverse_words" do
    specify { "hello".reverse_words.should eq("hello") }
    specify { "hello world".reverse_words.should eq("world hello") }
    specify { "bob & pop run".reverse_words.should eq("run pop & bob") }
  end
end

當我運行rspec string-additions.rb --color --format doc ,我得到:

String
  #reverse_words
    should == hello
    should == world hello
    should == run pop & bob

但是,我希望獲得合理的輸出,如下所示:

String
  #reverse_words
    "hello" => "hello"
    "hello world" => "world hello"
    "bob & pop run" => "run pop & bob"

而且,我想稍微干掉我的規格。 RSpec是否提供了用於干預這種多案例測試的模板? 類似黃瓜情景的概述

注意:這個問題類似於RSpec與Cucumber的“場景”中的等價物,還是我使用RSpec的方式錯誤? 但提供了一個應該用RSpec而不是Cucumber測試的例子。

在閱讀了Elisabeth Hendrickson的自動生成測試和RSpec冒險之后,我想出了這個解決方案:

describe String do
  describe "#reverse_words" do
    strings = {
      "hello"         => "hello",
      "hello world"   => "world hello",
      "bob & pop run" => "run pop & bob"
    }

    strings.each do |k, v|
      specify "\"#{k}\" => \"#{v}\"" do
        k.reverse_words.should eq(v)
      end
    end
  end
end

這給出了我想要的輸出,但是如果RSpec有一個模板可以讓事情變得更干凈,那就更好了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM