Skip to content

Instantly share code, notes, and snippets.

@DariaUlanova
Last active December 19, 2019 08:03
Show Gist options
  • Save DariaUlanova/99fc6e631942deecab761d595214998b to your computer and use it in GitHub Desktop.
Save DariaUlanova/99fc6e631942deecab761d595214998b to your computer and use it in GitHub Desktop.

Load test DB to be sure it is up-to-date.

(for example you want to run specs on CS then you need to load RS DB as CS uses rs models from RS)

$ bundle exec rake db:test:load

Run all existing specs, in CS, for instance

$ rspec

or run Guard to work with single spec (then save the spec file)

$ guard

CS and RE are used rs_models from RS.

So, CS and RE take rs factories from RS the same way: locally I added to CS & RE config a path to rs factories:

factories_path        '/Users/d/projects/livepepper/resource_server/spec/factories'

Currently there is no such path in config/application/config_test nor in CS master or in RE.

Instalation and settings for testing with Rspec for local test enviroment

Install gems

  group :test do
    gem 'database_cleaner' # https://github.com/DatabaseCleaner/database_cleaner
    gem 'factory_bot_rails' # https://github.com/thoughtbot/factory_bot_rails
    gem 'guard', require: false
    gem 'guard-bundler', require: false
    gem 'guard-rspec', require: false # https://github.com/guard/guard-rspec
    gem 'rspec'
    gem 'rspec-rails' # https://github.com/rspec/rspec-rails#rspec-rails---
    gem 'shoulda-matchers' # https://github.com/thoughtbot/shoulda-matchers
    gem 'vcr' # https://github.com/vcr/vcr#vcr
    gem 'webmock'
  end
$ bundle install

Rspec

$ rails generate rspec:install
      create  .rspec
      create  spec
      create  spec/spec_helper.rb
      create  spec/rails_helper.rb

Settings in RS

.rspec https://github.com/livepepper-dev/resource_server/blob/master/.rspec

spec/rails_helper.rb https://github.com/livepepper-dev/resource_server/blob/master/spec/rails_helper.rb#L1

spec/spec_helper.rb https://github.com/livepepper-dev/resource_server/blob/master/spec/spec_helper.rb

For CS, RE there are the same settings.

Guard

$ bundle exec guard init
      create  Guardfile

Guardfile in RS https://github.com/livepepper-dev/resource_server/blob/master/Guardfile

For CS, RE there are the same settings.

factory_bot_rails

In spec/rails_helper.rb

RS https://github.com/livepepper-dev/resource_server/blob/master/spec/rails_helper.rb

require 'factory_bot_rails'
Dir[Rails.root.join("spec/rs_factories/**/*.rb")].each { |f| require f } # path to factories
...

RSpec.configure do |config|
  ...
  config.include FactoryBot::Syntax::Methods
  ...
end

For CS, RE there are the same settings.

Currently some factories for rs_models are used in CS, RE, RS specs and they are stored in RS resource_server/spec/factories https://github.com/livepepper-dev/resource_server/tree/master/spec/factories

Most of them are with rs_ prefix for consistency with rs_models. But some old of them are still not renamed to not break specs.

shoulda-matchers

Provides specs with helper methods

For example, for testing AR models validation, relations with:

ActiveModel matchers https://github.com/thoughtbot/shoulda-matchers#activemodel-matchers

ActiveRecord matchers https://github.com/thoughtbot/shoulda-matchers#activerecord-matchers

or to test controllers:

ActionController matchers https://github.com/thoughtbot/shoulda-matchers#actioncontroller-matchers

database_cleaner

Provides strategies for cleaning test database by adding settings below to spec/rails_helper.rb

  config.before(:suite) do
    DatabaseCleaner.strategy = :transaction # to run each case in DB transaction
    DatabaseCleaner.clean_with(:truncation)
  end

RS https://github.com/livepepper-dev/resource_server/blob/master/spec/rails_helper.rb#L38

For CS, RE there are the same settings.

VCR

Installed and used only for RS.

In spec/rails_helper.rb

https://github.com/livepepper-dev/resource_server/blob/master/spec/rails_helper.rb#L22

VCR.configure do |config|
  config.cassette_library_dir = "spec/vcr_cassettes"
  config.hook_into :webmock
  config.configure_rspec_metadata!
  config.default_cassette_options = { record: :new_episodes, :match_requests_on => [:method, :uri, :body] }
end

new_episodes replay previously recorded interactions. Record new interactions for similar requests when some request data changed (will write a new request below the previous in cassette).

match_requests_on how VCR matches requests.

Cassettes directory in RS https://github.com/livepepper-dev/resource_server/tree/master/spec/vcr_cassettes

Example of spec with VCR in RS https://github.com/livepepper-dev/resource_server/blob/master/spec/models/sms/siminn_gateway_spec.rb#L1

Success case cassette https://github.com/livepepper-dev/resource_server/blob/master/spec/vcr_cassettes/Sms_SiminnGateway/run/Success/1_1_1_1.yml

Fail case cassette https://github.com/livepepper-dev/resource_server/blob/master/spec/vcr_cassettes/Sms_SiminnGateway/run/Fail/1_1_2_1.yml

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment