diff --git a/lib/mock_redis/hash_methods.rb b/lib/mock_redis/hash_methods.rb index 9239830..e79a7a0 100644 --- a/lib/mock_redis/hash_methods.rb +++ b/lib/mock_redis/hash_methods.rb @@ -10,6 +10,11 @@ def hdel(key, *fields) with_hash_at(key) do |hash| orig_size = hash.size fields = Array(fields).flatten.map(&:to_s) + + if fields.empty? + raise Redis::CommandError, "ERR wrong number of arguments for 'hdel' command" + end + hash.delete_if { |k, _v| fields.include?(k) } orig_size - hash.size end diff --git a/spec/commands/hdel_spec.rb b/spec/commands/hdel_spec.rb index d015569..418a6ec 100644 --- a/spec/commands/hdel_spec.rb +++ b/spec/commands/hdel_spec.rb @@ -66,5 +66,12 @@ @redises.get(@key).should be_nil end + it 'raises error if an empty array is passed' do + expect { @redises.hdel(@key, []) }.to raise_error( + Redis::CommandError, + "ERR wrong number of arguments for 'hdel' command" + ) + end + it_should_behave_like 'a hash-only command' end