I'm writing a Ruby wrapper for a web-based API and each request requires a unique transaction ID to be sent along with the request.
I have written up a test shell using MiniTest::Spec
, however the the transaction id is not incrementing between each test.
The test shell, leaving out tedious details, is as follows:
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
The puts
line in there shows however that the @txid
is not actually incrementing between each test.
A few more tests demonstrate that any assignment of a value to an instance variable within the body of a test has no effect on the value of the variable.
Is this expected? What is the correct way to handle this?