Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarity around using a construct in a before block for rspec #5

Open
programingnotes opened this issue Jan 9, 2019 · 6 comments
Open

Comments

@programingnotes
Copy link

programingnotes commented Jan 9, 2019

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!

@bhb
Copy link
Owner

bhb commented Jan 9, 2019

@programingnotes Thanks for using this gem! Sorry you're running into issues.

Can you post the source of the spec that is failing?

@programingnotes
Copy link
Author

programingnotes commented Jan 10, 2019

@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

@bhb
Copy link
Owner

bhb commented Jan 11, 2019

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.

@bhb
Copy link
Owner

bhb commented Jan 11, 2019

@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

@bhb
Copy link
Owner

bhb commented May 31, 2019

PRs are welcome to update the integration

Just wanted to highlight that I'd still welcome PRs for this

@cvoltz
Copy link
Contributor

cvoltz commented Mar 23, 2020

See #11 for a version of the example which works with a modern version of RSpec.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants