How to Integrate Cucumber Tests with Rails Event Store

May 07, 2019
3 min read
 How to Integrate Cucumber Tests with Rails Event Store - Cover photo
1. Introduction
This is a short tutorial on how to integrate cucumber tests with Rails Event Store
The gem for Event Store is "rails_event_store", which can be found here and for cucumber, it is "cucumber-rails", and can be found here
2.How to integrate Cucumber Tests with Rails Event Store

In order to demonstrate this, I’ve created a demo application that stores and saves data about an item, starting with its creation, the changes of its properties and its deletion. To do this, we have a file called events.rb where all these events are defined.

Let's start by adding the required gems inside Gemfile: rails_event_store, cucumber-rails, and rails_event_store-rspec, which is the gem used by Event Store for testing. Now we can run bundle so we can use them. 
  gem 'cucumber-rails', require: false
  gem 'database_cleaner'
  
  gem 'rails_event_store-rspec'
The rails_event_store_rspec gem provides some matchers such as have_published or  be_event, which can easily be used to ensure that the events are published correctly.
The next step is to include RailsEventStore::RSpec::Matchers into the cucumber setup file so we can access the matchers. We can also add a method inside this file, which we can call event_store, and which can be used to skip the call to Rails.configuration.event_store each time inside step definitions.
require 'cucumber/rails'

include ::RailsEventStore::RSpec::Matchers

def event_store
  Rails.configuration.event_store
end
The events registered inside events.rb are: ItemCreated, ItemUpdated and ItemDeleted that correspond to the three main states that an item can go through.
module Events
  ItemCreated = Class.new(RailsEventStore::Event)
  ItemUpdated = Class.new(RailsEventStore::Event)
  ItemDeleted = Class.new(RailsEventStore::Event)
end
Now that we have defined the events, we can create the step definition file and .feature file in order to test whether our events have been correctly published.
First, let’s add some data to an item. In our case, the uid and the name.
When an event is published, we can use the have_published method to check if the event was published once and if it was published on the right stream.
We can also check the number of events published for a specific stream.
Given("uid {int} and name {string}") do |uid, name|
  @data = { uid: uid, name: name }
end

When("{string} event is published") do |event|
  stream_name = "Item$#{@data[:uid]}"

  event = event_type(event).new(data: @data)
  event_store.publish(event, stream_name: stream_name)
end

Then("an {string} event should be published once for item {int}") do |event, uid|
  expect(event_store).to have_published(
    an_event(event_type(event))).in_stream("Item$#{uid}").once
end

Then("there should be {int} events published for item stream {string}") do | event_numbers, stream_name|
  expect(event_store.read.stream(stream_name).to_a.count).to eq event_numbers
end

def event_type(event)
  "Events::#{event}".constantize
end
Feature: The system tracks items as they go through different states
  Scenario: Check if ItemCreated event is published
            Given uid 1 and name "item_1"
            When "ItemCreated" event is published
            Then an "ItemCreated" event should be published once for item 1

  Scenario: Check if ItemUpdated event is published
            Given uid 2 and name "item_2"
            When "ItemUpdated" event is published
            Then an "ItemUpdated" event should be published once for item 2

  Scenario: Check the number of events published by an item
            Given uid 3 and name "item_3"
            When "ItemCreated" event is published
              And "ItemUpdated" event is published
              And "ItemDeleted" event is published
            Then there should be 3 events published for item stream "Item$3"

3. Conclusion
In this snippet, I tried to implement some basic examples that demonstrate the integration of Rails Event Store and cucumber tests, but these examples can be extended and improved. 
The entire implementation can be found on GitHub on this repository. You can also view the documentation for rails_event_store-rspechere.

Share on:

Want to stay on top of everything?

Get updates on industry developments and the software solutions we can now create for a smooth digital transformation.

* I read and understood the ASSIST Software website's terms of use and privacy policy.

Frequently Asked Questions

ASSIST Software Team Members

See the past, present and future of tech through the eyes of an experienced Romanian custom software company. The ASSIST Insider newsletter highlights your path to digital transformation.

* I read and understood the ASSIST Software website's terms of use and privacy policy.

Follow us

© 2024 ASSIST Software. All rights reserved. Designed with love.