test_construct
test_construct copied to clipboard
Clarity around using a construct in a before block for rspec
hi @bhb I am currently experiencing a situation similar to that discussed in this issue
describe '#create_domain_dir' do
context 'when directory does exist' do
before do
within_construct do |construct|
@created_here = file_creator.create_domain_dir(domain_name)
end
end
it 'returns said directory' do
within_construct do |construct|
created = file_creator.create_domain_dir(domain_name)
expect(created).to eq(@created_here)
end
end
end
end
the feedback in the issue referred to suggests to " use the RSpec integration and make calls in your setup block" but doing this results in an error:
RSpec::Core::ExampleGroup::WrongScopeError: `example` is not available from
within an example (e.g. an `it` block) or from constructs that run in the scope of
an example (e.g. `before`, `let`, etc). It is only available on an example group
(e.g. a `describe` or `context` block).
I have looked through rspec_integration but can't seem to figure out how to accomplish keeping the construct around till my example block runs.
Would appreciate feedback please. thank you!
@programingnotes Thanks for using this gem! Sorry you're running into issues.
Can you post the source of the spec that is failing?
@bhb thanks for getting back to me. By the source, I take it you mean the method being tested? If so then the pseudo code is pasted below:
class FileCreator
attr_reader :file_class, :file_utils_class
def initialize
@file_class = File
@file_utils_class = FileUtils
end
def create_domain_dir(domain_name)
domain_dir_as_string = live_dir + "/#{domain_name}"
if Dir.exist?(domain_dir_as_string)
domain_dir_as_string
else
file_utils_class.mkdir_p(domain_dir_as_string, :mode => 0755)
end
end
def live_dir
live_dir_as_string = Dir.pwd + '/intel/job/analysis'
end
end
Sorry, I should have been clearer: can you post your spec code in its entirety? The spec code in your original message appears to only be a section of the spec file.
In particular, I'd be curious to see the full code that yielded the "example" is not available from within an example (e.g. an
it block)
error.
@programingnotes Actually, I can reproduce the issue the RSpec integration. Sorry about that - not sure if it was always buggy or if some change in RSpec broke this. I haven't used this project in awhile (and I don't usually use RSpec in my projects), so it may have always had an issue - although it's weird the tests pass in this project.
In any case, it looks like the example as provided doesn't work with the modern version of RSpec - PRs are welcome to update the integration 😄
Of course, you could always setup/teardown construct manually in before
and after
blocks. It's not as nice, but it works.
require_relative 'file_creator'
require "test_construct/rspec_integration"
describe '#create_domain_dir' do
let(:domain_name) { "example.com" }
let(:file_creator) { FileCreator.new }
before do
@c = setup_construct
end
after do
teardown_construct(@c)
end
it 'works if directory does not exist' do
file_creator.create_domain_dir(domain_name)
end
it 'works if dir does exist' do
@c.directory "intel/job/analysis/example.com"
file_creator.create_domain_dir(domain_name)
end
end
PRs are welcome to update the integration
Just wanted to highlight that I'd still welcome PRs for this
See #11 for a version of the example which works with a modern version of RSpec.