Git Product home page Git Product logo

Comments (8)

yaauie avatar yaauie commented on June 29, 2024 1

@iainbeeston flushdb is implemented and correctly emulates its non-mock counterpart; it is the correct way to flush the db.

from mock_redis.

ascandella avatar ascandella commented on June 29, 2024

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.

iainbeeston avatar iainbeeston commented on June 29, 2024

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.

 avatar commented on June 29, 2024

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.

iainbeeston avatar iainbeeston commented on June 29, 2024

Yes - that's exactly what I'm doing! My question was more:

  1. What is the correct command to clear the database with mockredis
  2. 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.

lann avatar lann commented on June 29, 2024

@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.

yaauie avatar yaauie commented on June 29, 2024

@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.

lann avatar lann commented on June 29, 2024

@yaauie:

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.