- Read Tutorial
If you're taking an advanced Rails course you probably already know how to install RSpec for Behavioral Driven Development (BDD), however we'll go through it in case it's new to it or you want a resource with clear instructions. You can also reference my rspec-rails gem tutorial at anytime.
The first thing to do will be to install the gem, update the :development, :test
block in the Gemfile
and add in the following gems
# Gemfile group :development, :test do gem 'rspec-rails', '~> 3.4', '>= 3.4.2' gem 'factory_girl_rails' gem 'capybara' gem 'database_cleaner' end
In addition to rspec-rails
I'm also integrating:
factory_girl_rails
- for creating test data objectscapybara
- for creating integration testsdatabase_cleaner
- for cleaning out the database and prevent data conflicts
After running bundle
we can run the RSpec
generator:
rails generate rspec:install
This will give us our test files and integrate RSpec
so that it automatically creates specs for our models and controllers when we run rails generators. Now let's customize our rails_helper
file, I've removed all of the comments and added in calls for capybara
and integrated the database cleaning processes:
# spec/rails_helper.rb ENV["RAILS_ENV"] ||= 'test' require 'spec_helper' require File.expand_path("../../config/environment", __FILE__) require 'rspec/rails' require 'capybara/rails' Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f } ActiveRecord::Migration.maintain_test_schema! RSpec.configure do |config| config.include FactoryGirl::Syntax::Methods config.fixture_path = "#{::Rails.root}/spec/fixtures" config.use_transactional_fixtures = false config.before(:suite) { DatabaseCleaner.clean_with(:truncation) } config.before(:each) { DatabaseCleaner.strategy = :transaction } config.before(:each, :js => true) { DatabaseCleaner.strategy = :truncation } config.before(:each) { DatabaseCleaner.start } config.after(:each) { DatabaseCleaner.clean } config.infer_spec_type_from_file_location! end
That's all that we have to do, let's create our resource to ensure that everything is working properly.
Creating a Microservice Model with BDD
Run the following resources
generator in the terminal, you can reference the application requirements to review the attributes that we'll need to have:
rails g resource Notification phone:string body:text source_app:string
Running this will create all Notification
files, including the: Model and Controller files:
MacBook-Pro-3:message_service admin$ rails g resource Notification phone:string, body:text, source_app:string Running via Spring preloader in process 49023 invoke active_record create db/migrate/20160212022834_create_notifications.rb create app/models/notification.rb invoke rspec create spec/models/notification_spec.rb invoke factory_girl create spec/factories/notifications.rb invoke controller create app/controllers/notifications_controller.rb invoke erb create app/views/notifications invoke rspec create spec/controllers/notifications_controller_spec.rb invoke helper create app/helpers/notifications_helper.rb invoke rspec create spec/helpers/notifications_helper_spec.rb invoke assets invoke coffee create app/assets/javascripts/notifications.coffee invoke scss create app/assets/stylesheets/notifications.scss invoke resource_route route resources :notifications
Notice how it also created a number of 'spec' files? That means that our RSpec
integration is working and automatically creating spec files for us to use.
Run rake db:migrate
to update the schema
file and we'll be ready to create our first spec.
Open up the notification
model spec and update it with a basic test:
# spec/models/notification.rb require 'rails_helper' RSpec.describe Notification, type: :model do describe 'creation' do it 'can be created' do notification = Notification.create(phone: "555-555-5555", body: "My message", source_app: "some_app") expect(notification).to be_valid end end end
This is a very basic test that ensures that RSpec
is working by checking to see if a Notification
can be created. Running rspec
in the terminal will run the spec and it looks like everything is working! Nice work, we have our test suite ready to go and now we can start building out our microservice.