Skip to content

Instantly share code, notes, and snippets.

@enkessler
Last active August 24, 2020 07:56
Show Gist options
  • Save enkessler/beb0bc2c7707ab8ae7ee23f30524df7a to your computer and use it in GitHub Desktop.
Save enkessler/beb0bc2c7707ab8ae7ee23f30524df7a to your computer and use it in GitHub Desktop.
This gist demonstrates one way to write a Cucumber scenario that tests an asynchronous feature. It is essentially a two part test but it maintains the documentation value of a single, cohesive test.
Feature: Testing an asynchronous thing
@async @batch_1
Scenario: Some big batch process
Given some stuff is set up
When the thing happens
Then it worked
@async @batch_2
Scenario: Another big batch process
Given some stuff is set up
When a different thing happens
Then it worked
async: -t @async
require 'yaml'
# Somehow figure out if the external processes have occurred. Using a flag file as an example.
Before('@async') do |scenario|
test_tags = scenario.source_tag_names
relevant_flag = test_tags.select { |tag| tag =~ /batch/ }
flags = File.open('flag_file.yml') { |file| YAML.load file }
@thing_has_happened = flags[relevant_flag]
end
# This hook just toggles the flag in order to demonstrate both code paths. Presumably some
# outside process would update the flag file once the triggered action is finished.
After('@async') do |scenario|
test_tags = scenario.source_tag_names
relevant_flag = test_tags.select { |tag| tag =~ /batch/ }
flag_hash = File.open('flag_file.yml') { |file| YAML.load file }
flag_hash[relevant_flag] = !@thing_has_happened
File.write('flag_file.yml', flag_hash.to_yaml)
end
---
? - ! '@batch_1'
: false
? - ! '@batch_2'
: false
Given(/^some stuff is set up$/) do
# Do whatever you need to set things up
end
When(/^the thing happens$/) do
unless @thing_has_happened
# Possibly trigger something
# All done for now
pending
else
# Possibly do some more stuff
end
end
Then(/^it worked$/) do
# Check for success
end
When(/^a different thing happens$/) do
unless @thing_has_happened
# Possibly trigger something
# All done for now
pending
else
# Possibly do some more stuff
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment