Comments (8)
@iainbeeston flushdb
is implemented and correctly emulates its non-mock counterpart; it is the correct way to flush the db.
from mock_redis.
That's an interesting question, and I think it comes down to how you test. Do you run all your database tests in transactions, such that the database is at a known state between each test?
At @causes, we do neither: we expect that our tests can deal with whatever the state of memcache (which we also mock out), redis, and our database happen to be in. We do find that this sometimes uncovers failures in seemingly unrelated tests, but we take that as an opportunity to further separate dependencies.
tl;dr no, I don't think it's important to suggest that everyone flushes their database between specs
from mock_redis.
I use database cleaner (the gem) to truncate my database between each test,
which in practice is similar to running each inside a transaction. In
effect we have an assumption that at the start if each test there are no
records in the database. I had originally expected that database cleaner
would have redid support, but it doesn't. So what I'm looking for is the
best way of reproducing the same behaviour using (mock) redis.
TL;DR I'd ideally like to be able to clean out the mock redis database
between test runs in the style of database cleaner
Iain
from mock_redis.
If you're using RSpec you should be able to do something like this, and I expect other test frameworks have similar hooks:
RSpec.configure do |config|
config.before(:each) do
# flush db in here
end
end
mock_redis shouldn't necessarily enforce the clean-before-every-example style, but it shouldn't get in the way of people who want to apply the pattern.
from mock_redis.
Yes - that's exactly what I'm doing! My question was more:
- What is the correct command to clear the database with mockredis
- Should the previous answer go into the readme, in case others have the
same question? (It's a fairly common practice I think)
Sorry if it sounded like I wanted mockredis to do this automatically. It'd
be completely out of scope, if not impossible to implement at this level.
Iain
from mock_redis.
@iainbeeston one issue with including an example is that the exact code will depend on how you are using redis - in particular you will need some global access to the Redis (MockRedis) object in order to call flushdb. It might be appropriate to implement something like MockRedis.flushdb_all!
by tracking all instantiated MockRedis objects and calling flushdb on each.
from mock_redis.
@lann If you can't keep track of which objects you're working with, you're going to have problems; keeping a reference to the MockRedis
instances in MockRedis
namespace means they will never go out of scope and will therefore never be garbage-collected. This would be very surprising in the following scenario, where you would expect your instances to be GC'd:
describe 'something' do
let(:redis_connecton) { MockRedis.new }
subject { redis_connection }
1000.times do |i|
it { should be_a_kind_of? MockRedis }
end
end
from mock_redis.
If you can't keep track of which objects you're working with, you're going to have problems
True; my point is that every project will be tracking the Redis object differently, so the example code wouldn't be a drop-in fix.
they will never go out of scope and will therefore never be garbage-collected
Good point; weak refs could handle that.
from mock_redis.
Related Issues (20)
- Ruby 3.0 support HOT 1
- Support for M1
- Not mention XADD was supported for this gem, right? HOT 2
- Add :get keyword to #set method. HOT 1
- Moneta 1.5 support HOT 1
- Unknown keywords: :path, ... after updating to redis-rb 4.7.0
- Release new version HOT 2
- Use MockRedis with Redis::Connection.drivers HOT 3
- deprecation warning
- Incompatible with the new 'redis-client' gem HOT 8
- ArgumentError after update ruby from 3.1.2 to 3.2.0 HOT 4
- Add #sadd? to match redis-rb v4.8.0 HOT 1
- Add additional arg to lpop to allow popping multiple records
- TypeError when using `eval()` HOT 1
- Add support for zadd ch, gt, lt options
- Unable to execute `#call` with redis command in `pipelined` HOT 1
- Add :exat and :pxat keywords to #set method HOT 1
- From redis-rb 5.0.0, `sadd` and `srem` always return integers HOT 2
- Add support for `EXPIRE` command options HOT 1
- ArgumentError: wrong number of arguments (given 3, expected 2) When used in conjunction with redis-store HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mock_redis.