mardi 2 octobre 2018

Ruby: Pessimistic locking is not working properly with reload

Pessimistic locking is not working properly with reload (used in the test cases). with_lock and reload is not working correctly test cases are getting failed. If I removed the with_lock then test work fine.

def method_name1
  self.with_lock do
    attributes["amount_used"] = get_total_amount_used
    attributes["updated_at"] = Time.now.utc
    Product.where(:id => self.id).update_all(attributes)
  end
end

I have written unit test cases using FactoryGirl. It calls the method_name1 which recalculate the total after the used amount. If you check the first Test case 1: total = 600 and amount_used = -200 (-ve means reducing), now total should be 400.00. Similarly for "Test case 2", After running "Test case 1" total is 400.00. amount_used = 200 (+ve means add), total should be 600.00. But Test Case 2 is saying 800.00.

context 'description' do
  before(:each) do
    @product.total = 600
    @product.method_name1
  end

  it 'is updated automatically if valid' do
    @product.amount_used = -200
    @product.save
    @product.reload
    should eq('400.0')       #Working
  end

  it 'is not changed if invalid' do
    @product.amount_used = 200
    @product.save
    @product.reload
    should eq('600.0')      #Not Working
  end
end

Error For Test Case 2: Failure/Error: should eq('600.0')

   expected: "600.0"
        got: "800.0"

   (compared using ==)

Aucun commentaire:

Enregistrer un commentaire