I'm writing specs for a PDF generated with RoR & Prawn. There are a bunch of filter options (24 of them) for that PDF. In order to not miss any important specs I'm generating the filter options in a before :context block and saving them in an instance variable.
Where my problem starts is when trying to iterate over all of the filter options and run shared examples, to test for basics that don't change much with different filters.
This is what my code looks like for all these filters and the documentations filter (the filter_setting method is just a helper to access specific @major_filter_options):
describe 'pdf' do
before :context do
@major_filter_options = {}
@major_filter_options.define_them
end
describe 'basic content' do
before :context do
@filter_options_with_docu = {}
# find all the filters that have the docu option enabled
@major_filter_options.each do |key, mfo|
@filter_options_with_docu[key] = mfo if key.to_s.include? 'docu'
end
end
24.times do |t| # can't access major_filter_options.size here.. it's nil.
include_examples 'first_3_pages' do
let(:pdf) do
filter_options = filter_setting(@major_filter_options.keys[t])
ProjectReport::ReportGenerator.new.generate(project, filter_options, user).render
end
let(:page_analysis) { PDF::Inspector::Page.analyze(pdf) }
end
end
12.times do |t| # @print_options_with_docu is also nil at this point
include_examples 'documentation_content' do
let(:pdf) do
filter_options = filter_setting(@filter_options_with_docu.keys[t])
ProjectReport::ReportGenerator.new.generate(project, filter_options, user).render
end
let(:page_analysis) { PDF::Inspector::Page.analyze(pdf) }
end
end
# ...
end
I have 2 big problems:
One is that this 24.times, 12.times and so on (there's a bunch of them) are bothering me because it makes maintainance a lot harder. A new filter option would change all the values, and finding all of the values to change them is very susceptible to mistakes in my opinion.
The other problem is the fact that the variable iterated here like this:
12.times do |t| doesn't actually seem to iterate when I'm inside of any of these let's:
let(:pdf) do
filter_options = filter_setting(@major_filter_options.keys[t])
puts t
# ...
end
The puts t here will print 11 every time (the filter is also the same every time).
After some reading I found a gist example. The problem looked similar enough, but sadly puting a describe block around it didn't do much.
24.times do |t|
describe
# same as before
end
end
Interestingly enough though, when doing puts t again in that setup, it would be 6 every time, which left me a little more confused.
I should also mention, that the reason for splitting them up like this is that I have shared examples that only apply for certain filters. If someone has a better idea on how to, for example iterate over the @major_filter_options and then just call certain shared examples depending on the current hash key, I'm all ears!
include_examplesworks only once per context. I think you have to surround it with a uniq context likecontext "...#{t}" do