ruby - Incrementing an instance variable in the before :each clause of a MiniTest -
i'm writing ruby wrapper web-based api , each request requires unique transaction id sent along request.
i have written test shell using minitest::spec
, the transaction id not incrementing between each test.
the test shell, leaving out tedious details, follows:
describe "should expected responses server" before :all # random number between 1 , maxint - 100 @txid = 1 + securerandom.random_number(2 ** ([42].pack('i').size * 8 - 2) - 102) @username = securerandom.urlsafe_base64(20).downcase end before :each # increment txid @txid += 1 puts "new txid #{@txid}" end "should blah blah" # test uses @txid end "should blah blah blah" # different test uses incremented @txid end end
the puts
line in there shows @txid
not incrementing between each test.
a few more tests demonstrate assignment of value instance variable within body of test has no effect on value of variable.
is expected? correct way handle this?
minitest doesn't support before :all
way rspec does. type pass before do
(such :all
or :each
) ignored in underlying implementation.
see this issue relevant discussion, , note the docs specify that: "type ignored , there make porting easier."
you use class variables (not beautiful, they'd meet needs here). alternatively, looks can set custom runner if use minitest::unit - check out the docs, this older answer, , this gist more details.
Comments
Post a Comment