1. Introduction
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.
gem 'cucumber-rails', require: false gem 'database_cleaner' gem 'rails_event_store-rspec'
require 'cucumber/rails' include ::RailsEventStore::RSpec::Matchers def event_store Rails.configuration.event_store end
module Events ItemCreated = Class.new(RailsEventStore::Event) ItemUpdated = Class.new(RailsEventStore::Event) ItemDeleted = Class.new(RailsEventStore::Event) end
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"