Ruby on Rails Thursday, August 9, 2018

We have a Lockable concern that allows for locks via Redis


module Lockable    extend ActiveSupport::Concern      def redis_lock(key, options = {})      Redis::Lock.new(        key,        expiration: options[:expiration] || 15,        timeout: options[:timeout] || 0.1      ).lock { yield if block_given? }    end  end



We use this in a Controller method to ensure concurrent requests are handled correctly.


def create    redis_lock(<generated_key>, timeout: 15) do      # perform_operation    end      render json: <data>, status: :ok  end


When testing this action, I want to test that the correct generated_key is being sent to Redis to initiate a lock.


I set up an expect for the Redis::Lock but that returns false always presumably because the request to create is sent mid request and not at the end of it.

expect(Redis::Lock).to receive(:create).once



Since the lock is cleared at the end of the method call, I cannot check for the key in redis as a test.


This answer recommends setting up a fake class that matches the structure of Lockable to emulate the same behaviour but how do I write a test for it? The method we have does not return any value to verify.


<SO link to the same question>

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/4e12ede9-45b0-41ee-8309-50b9a6bbfe04%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment