Ruby 在rspec中生成示例id

Ruby 在rspec中生成示例id,ruby,rspec2,Ruby,Rspec2,我需要帮助找出如何为我的rspec测试中的每个示例生成唯一标识符。要使下面的代码正常工作,我需要做哪些更改 describe 'Verify that my server' do @x = 1 it "does something " + @x.to_s do 2.should==2 end it "does something else " + @x.to_s do 2.should==2 end afte

我需要帮助找出如何为我的rspec测试中的每个示例生成唯一标识符。要使下面的代码正常工作,我需要做哪些更改

describe 'Verify that my server' do

    @x = 1

    it "does something " + @x.to_s do
        2.should==2
    end

    it "does something else " + @x.to_s do
        2.should==2
    end

    after(:each) do
        @x+=1
    end
end

Rspec中的每个示例都应该完成一个句子,这个句子通常从
描述
块开始,封装相关测试

我从我自己的一个规格中取了这个:

describe Redis::BigHash do
  before :each do
    @hash = Redis::BigHash.new
    @hash[:foo] = "bar"
    @hash[:yin] = "yang"
  end

  describe "#[]" do
    it "should read an existing value" do
      @hash[:foo].should == "bar"
    end
    it "should get nil for a value that doesn't exist" do
      @hash[:bad_key].should be_nil
    end
    it "should allow lookup of multiple keys, returning an array" do
      @hash[:foo, :yin, :bad_key].should == ["bar", "yang", nil]
    end
  end
end
你会得到这样的句子:

  • Redis::BigHash#[]
    应读取现有值
  • Redis::BigHash#[]
    对于不存在的值,应该得到nil
  • Redis::BigHash#[]
    应允许查找多个键,并返回一个数组
简单的英语句子描述你想要的行为。

看看如何在测试中生成随机值。它可以生成真实的随机数据,如电子邮件地址、IP地址、电话号码、人名等,但它也有生成随机字母和数字字符串的基本方法

Faker.numerify("###-###-###")
# => 123-456-789

或者,您也可以使用stdlib。

友好的挑剔,例如:
Redis::BigHash.[]读取现有值
,如果省略“应该”,那么消息的可读性会更好。我想知道指定的行为,而不是假设的行为:)为stackoverflow格式化了ruby代码;)便利提示,将代码粘贴到问题中,然后选择它并按ctrl+K(或Mac OS上的CMD+K)