Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing tests with RSpec for Redis with Rails

I have a model class that caches data in redis. The first time I call a method on the model, it computes a JSON/Hash value and stores it in Redis. Under certain circumstances I 'flush' that data and it gets recomputed on the next call.

Here's the code snippet similar to the one I use to store the data in Redis:

def cache_data
  self.data_values = data_to_cache
  REDIS.set(redis_key,ActiveSupport::JSON.encode(self.data_values))
  REDIS.get(redis_key) 
end

def data_to_cache
  # generate a hash of values to return
end

How should I unit test this code? I use RSpec and Capybara. I also use Cucumber and Capabara for integration testing if that helps.

like image 650
Kevin Bedell Avatar asked May 08 '12 15:05

Kevin Bedell


People also ask

How to run RSpec tests on a Rails application?

Run bundle install in the terminal to install the gems specified in your gemfile. Run rails generate rspec:install to generate the spec files necessary for running your RSpec tests. This includes files for testing your models, controllers, and helpers and can be found in the newly created spec folder. 2. Create an Instance to Test

How to add RSpec-Rails helper to the Gemfile?

Add the rspec-rails helper to the Gemfile: # Gemfile . . . group :development, :test do gem 'rspec-rails', ">= 3.9.0" end Install the Gems and complete the setup: $ bundle install --path vendor/bundle $ bin/rails generate rspec:install $ bin/rails webpacker:install Let’s generate this model using Rails generator:

What is the best testing tool for Ruby on rails?

RSpec is the most common testing tool for Ruby. It was created for behaviour-driven development (BDD), meaning that tests should be specified in terms of the desired behaviour of the program. If you want to learn more about BDD and RSpec, you can find a good overview here.

What is RSpec and why do we need it?

Aside from the DSL that Rails provide for the model, RSpec also provides us with model specs, which allow us to properly unit test our models. We saw how we can tackle issues in the BDD way, by using specs and test-driven development.


2 Answers

I like to have redis running while the tests are running. Redis, unlike e.g. postgres, is extremely fast and doesn't slow down test run time noticeably.

Just make sure you call REDIS.flush in a before(:each) block, or the corresponding cucumber hook.

You can test data_to_cache independently of redis, but unless you can fully trust the redis driver you're using and the contract it provides, it's safer to actually test cache_data (and the corresponding cache fetch method) live. That also allows you to switch to a different redis driver (or to a different fast KV store) without a wholesale rewrite of your tests.

like image 169
Jay Moorthi Avatar answered Sep 28 '22 04:09

Jay Moorthi


First of all add the below code in the spec_helper.rb so you'll be sure that the tests will run on any machine even if the redis server is not installed (make sure to add to your gemfile mock_redis under test scopes:

redis_instance = MockRedis.new
Redis.stub(:new).returns(redis_instance)
Redis::Store.stub(:new).returns(redis_instance)

After that I would test:

  1. The data written to REDIS is the expected data
  2. A sequence of cache_data, flush_data, cache_data calls the data_to_cache twice
like image 45
Cristian Bica Avatar answered Sep 28 '22 03:09

Cristian Bica