0

私は Web ベースの API 用の Ruby ラッパーを作成しています。各要求には、要求と共に送信される一意のトランザクション ID が必要です。

を使用してテスト シェルを作成しましたがMiniTest::Spec、各テスト間でトランザクション ID が増加しません。

面倒な詳細を省略したテスト シェルは次のとおりです。

describe "should get the expected responses from the server" do
  before :all do
    # random number between 1 and maxint - 100
    @txid = 1 + SecureRandom.random_number(2 ** ([42].pack('i').size * 8 - 2) - 102)
    @username = SecureRandom.urlsafe_base64(20).downcase
  end

  before :each do
    # increment the txid
    @txid += 1
    puts "new txid is #{@txid}"
  end

  it "should blah blah" do
    # a test that uses @txid
  end

  it "should blah blah blah" do
    # a different test that uses the incremented @txid
  end
end

putsただし、そこにある行は、が@txid実際には各テスト間で増加していないことを示しています。

さらにいくつかのテストは、テスト本体内のインスタンス変数への値の割り当てが変数の値に影響を与えないことを示しています。

これは期待されていますか?これを処理する正しい方法は何ですか?

4

2 に答える 2

2

Minitest は、テスト クラスの個別のインスタンスで各テストを実行します。このため、インスタンス変数はテスト間で共有されません。テスト間で値を共有するには、グローバル変数またはクラス変数を使用できます。

describe "should get the expected responses from the server" do
  before do
    # random number between 1 and maxint - 100
    @@txid ||= SecureRandom.random_number(2 ** ([42].pack('i').size * 8 - 2) - 102)
    @@txid += 1 # increment the txid
    puts "new txid is #{@txid}"

    @@username ||= SecureRandom.urlsafe_base64(20).downcase
  end

  it "should blah blah" do
    # a test that uses @@txid
  end

  it "should blah blah blah" do
    # a different test that uses the incremented @@txid
  end
end

可能ですが、これはおそらく良い考えではありません。:)

于 2013-09-10T20:19:44.797 に答える