RSpec and feature toggles
For the last two years I use rspec for testing my code and I must admit that for most of the time I like this tool. As every high-performing team also my team is using feature toggle with help of gem called feature (love this self-explanatory names!).
Because feature toggle
uses database as store for toggles. At the begging almost every spec testing
feature hidden behind feature toggle started with ugly around
or even uglier
with before
and after
creating toggle and clean it up after test.
rspec can be easily extensible using example metadata. I decided to use this feature to control toggles enabled in scope of test. I defined tiny configuration helper:
RSpec.configure do |config|
config.around(:each, feature_toggle: /.+/) do |example|
toggles = Array.wrap(example.metadata[:feature_toggle]).map(&:to_sym)
Feature.run_with_activated(*toggles) do
example.run
end
end
end
Which can be used as follows:
context "when toggle `foo_bar` is activated", feature_toggle: :foo_bar do
it "something"
it "something else"
end
Pretty neat, right?