RSpec场景:多个测试用例 [英] RSpec Scenario Outlines: Multiple Test Cases

查看:164
本文介绍了RSpec场景:多个测试用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用RSpec测试一系列不同测试用例的最佳方法是什么?



例如,给定 string-addsions.rb

  require'rspec'

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

describe String do
describe#reverse_wordsdo
指定{hello.reverse_words.should eq(hello)}
{hello world.reverse_words.should eq(world hello)}
指定{bob& pop run.reverse_words.should eq(run pop& bob)}
end当我运行 rspec string-additions.rb时,
end

-color --format doc ,我获得:

  String 
#reverse_words
should == hello
should == world hello
should == run pop&但是,我想得到合理的输出,如下:

$ b


$ b

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

而且,我想要 DRY 我的规格有点。 RSpec是否提供了一个模板用于DRYing这种多种情况测试?类似于黄瓜场景概述的内容吗?



注意:此问题类似于在RSpec中是否有一个等价的对象到Cucumber的Scenarios或者我使用RSpec的方式错误?但是提供了一个例子,应该使用RSpec而不是Cucumber进行测试。 p>

解决方案

阅读 Elisabeth Hendrickson的Adventures with Auto-Generated Tests和RSpec ,我想出了这个解决方案:

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

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

但如果RSpec有一个模板,使事情甚至DRYer,它会更好。


What's the best way to test a bunch of different test cases with RSpec?

For example, given 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

when I run rspec string-additions.rb --color --format doc, I get:

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

However, I'd like to get sensible output, like this:

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

And, I'd like to DRY up my specs a bit. Does RSpec provide a template for DRYing up this sort of multiple-case testing? Something similar to Cucumber scenario outlines?

Note: This question is similar to Is there an equivalent in RSpec to Cucumber's "Scenarios" or am I using RSpec the wrong way? but provides an example that should be tested with RSpec rather than Cucumber.

解决方案

After reading Elisabeth Hendrickson's Adventures with Auto-Generated Tests and RSpec, I came up with this solution:

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

This gives the output I want, but it'd be nicer if RSpec had a template to make things even DRYer.

这篇关于RSpec场景:多个测试用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆