是否有一个gem来测试Rails中的Redis逻辑?

时间:2022-07-31 00:13:09

Like Database cleaner, or the default clearing of the data store after a test run. I searched and couldn't find one. It could be either a separate test data store or just something simple that namespaces all Redis commands into a test namespace.

比如数据库清理器,或者测试运行后的数据存储的默认清除。我找了半天也没找到。它可以是单独的测试数据存储,也可以是将所有Redis命令命名为测试名称空间的简单方法。

If anyone knows of any lemme know, otherwise I'll write one and OS it :)

如果有人知道任何lemme知道,否则我会写一个和操作它:)

3 个解决方案

#1


19  

When working with rails and redis I use a different redis db or namespace for the different environments. The setup is very simple and similar to ActiveRecords database config.

在使用rails和redis时,我对不同的环境使用不同的redis db或命名空间。该设置非常简单,类似于ActiveRecords数据库配置。

First, create a config: (namespace version commented out)

首先,创建一个config:(命名空间版本注释掉)

#config/redis.yml
default:
  host: localhost
  port: 6379
development:
  db: 0
#  namespace: appname_dev
test:
  db: 1
#  namespace: appname_test
production:
  db: 2
  host: 192.168.1.100
#  namespace: appname_prod

Then load the config and connect to redis through an initializer:

然后加载配置,通过初始化器连接到redis:

#config/initializers/redis.rb
REDIS_CONFIG = YAML.load( File.open( Rails.root.join("config/redis.yml") ) ).symbolize_keys
dflt = REDIS_CONFIG[:default].symbolize_keys
cnfg = dflt.merge(REDIS_CONFIG[Rails.env.to_sym].symbolize_keys) if REDIS_CONFIG[Rails.env.to_sym]

$redis = Redis.new(cnfg)
#$redis_ns = Redis::Namespace.new(cnfg[:namespace], :redis => $redis) if cnfg[:namespace]

# To clear out the db before each test
$redis.flushdb if Rails.env == "test"

Remember to add 'redis-namespace' to your Gemfile if your using that version.

如果您使用的是Gemfile,请记住添加“redis-namespace”。

#2


3  

You can try fakeredis. It is an fake redis implementation written in pure ruby.

你可以试试fakeredis。它是用纯ruby编写的伪redis实现。

#3


3  

Oh, yes there is. I use it in all of my projects where I need to test Redis logic.

哦,是的。我在所有需要测试Redis逻辑的项目中都使用它。

it's very useful, and it's not on the same database as your local development so the data has no danger of being "mixed"

它非常有用,而且与本地开发不在同一个数据库上所以数据不会有“混合”的危险

there it is: I put this code in my spec_helper.rb file, but you can put it in your test_helper.rb if you are using test unit.

这就是:我把这个代码放在spec_helper中。rb文件,但可以放在test_helper中。如果你使用的是测试单元。

# ==========================> Redis test configuration
REDIS_PID = "#{Rails.root}/tmp/pids/redis-test.pid"
REDIS_CACHE_PATH = "#{Rails.root}/tmp/cache/"

Dir.mkdir "#{Rails.root}/tmp/pids" unless Dir.exists? "#{Rails.root}/tmp/pids"
Dir.mkdir "#{Rails.root}/tmp/cache" unless Dir.exists? "#{Rails.root}/tmp/cache"

config.before(:suite) do
  redis_options = {
    "daemonize"     => 'yes',
    "pidfile"       => REDIS_PID,
    "port"          => 9736,
    "timeout"       => 300,
    "save 900"      => 1,
    "save 300"      => 1,
    "save 60"       => 10000,
    "dbfilename"    => "dump.rdb",
    "dir"           => REDIS_CACHE_PATH,
    "loglevel"      => "debug",
    "logfile"       => "stdout",
    "databases"     => 16
  }.map { |k, v| "#{k} #{v}" }.join('\n')
  `echo '#{redis_options}' | redis-server -`
end

config.after(:suite) do
  %x{
    cat #{REDIS_PID} | xargs kill -QUIT
    rm -f #{REDIS_CACHE_PATH}dump.rdb
  }
end

#1


19  

When working with rails and redis I use a different redis db or namespace for the different environments. The setup is very simple and similar to ActiveRecords database config.

在使用rails和redis时,我对不同的环境使用不同的redis db或命名空间。该设置非常简单,类似于ActiveRecords数据库配置。

First, create a config: (namespace version commented out)

首先,创建一个config:(命名空间版本注释掉)

#config/redis.yml
default:
  host: localhost
  port: 6379
development:
  db: 0
#  namespace: appname_dev
test:
  db: 1
#  namespace: appname_test
production:
  db: 2
  host: 192.168.1.100
#  namespace: appname_prod

Then load the config and connect to redis through an initializer:

然后加载配置,通过初始化器连接到redis:

#config/initializers/redis.rb
REDIS_CONFIG = YAML.load( File.open( Rails.root.join("config/redis.yml") ) ).symbolize_keys
dflt = REDIS_CONFIG[:default].symbolize_keys
cnfg = dflt.merge(REDIS_CONFIG[Rails.env.to_sym].symbolize_keys) if REDIS_CONFIG[Rails.env.to_sym]

$redis = Redis.new(cnfg)
#$redis_ns = Redis::Namespace.new(cnfg[:namespace], :redis => $redis) if cnfg[:namespace]

# To clear out the db before each test
$redis.flushdb if Rails.env == "test"

Remember to add 'redis-namespace' to your Gemfile if your using that version.

如果您使用的是Gemfile,请记住添加“redis-namespace”。

#2


3  

You can try fakeredis. It is an fake redis implementation written in pure ruby.

你可以试试fakeredis。它是用纯ruby编写的伪redis实现。

#3


3  

Oh, yes there is. I use it in all of my projects where I need to test Redis logic.

哦,是的。我在所有需要测试Redis逻辑的项目中都使用它。

it's very useful, and it's not on the same database as your local development so the data has no danger of being "mixed"

它非常有用,而且与本地开发不在同一个数据库上所以数据不会有“混合”的危险

there it is: I put this code in my spec_helper.rb file, but you can put it in your test_helper.rb if you are using test unit.

这就是:我把这个代码放在spec_helper中。rb文件,但可以放在test_helper中。如果你使用的是测试单元。

# ==========================> Redis test configuration
REDIS_PID = "#{Rails.root}/tmp/pids/redis-test.pid"
REDIS_CACHE_PATH = "#{Rails.root}/tmp/cache/"

Dir.mkdir "#{Rails.root}/tmp/pids" unless Dir.exists? "#{Rails.root}/tmp/pids"
Dir.mkdir "#{Rails.root}/tmp/cache" unless Dir.exists? "#{Rails.root}/tmp/cache"

config.before(:suite) do
  redis_options = {
    "daemonize"     => 'yes',
    "pidfile"       => REDIS_PID,
    "port"          => 9736,
    "timeout"       => 300,
    "save 900"      => 1,
    "save 300"      => 1,
    "save 60"       => 10000,
    "dbfilename"    => "dump.rdb",
    "dir"           => REDIS_CACHE_PATH,
    "loglevel"      => "debug",
    "logfile"       => "stdout",
    "databases"     => 16
  }.map { |k, v| "#{k} #{v}" }.join('\n')
  `echo '#{redis_options}' | redis-server -`
end

config.after(:suite) do
  %x{
    cat #{REDIS_PID} | xargs kill -QUIT
    rm -f #{REDIS_CACHE_PATH}dump.rdb
  }
end