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

Running rspec from within the spec directory #779

Open
gerases opened this issue Dec 31, 2019 · 2 comments
Open

Running rspec from within the spec directory #779

gerases opened this issue Dec 31, 2019 · 2 comments

Comments

@gerases
Copy link

gerases commented Dec 31, 2019

Hi, it seems that in order to run rspec-puppet tests, i need to be outside of the spec directory. When I'm running rspec from within the spec directory (./spec, ./spec/classes, etc), I get errors like these:

Failure/Error: it { is_expected.to compile }

     NameError:
       undefined local variable or method `compile'

I've tried to look through the code to understand why but wasn't very successful.

@rodjek
Copy link
Owner

rodjek commented Jan 6, 2020

Hi @gerases, this is an expected behaviour from rspec-core itself. RSpec assumes that you're running your tests from the root directory of your project and automatically adds the lib and spec directories to the Ruby load path.

I'm guessing to get the output above you commented out the require 'spec_helper' line at the top of your spec file? Otherwise I would have expected you to get output like

semirhage :0: puppetlabs-motd/spec (git:master → upstream)$ bundle exec rspec classes/motd_spec.rb

An error occurred while loading ./classes/motd_spec.rb.
Failure/Error: require 'spec_helper'

LoadError:
  cannot load such file -- spec_helper
# ./classes/motd_spec.rb:1:in `require'
# ./classes/motd_spec.rb:1:in `<top (required)>'
No examples found.

It is possible to work around this behaviour and I can help walk you through if you want, it but it's not really worth it in my opinion when it's easy enough to just run the tests from the project root.

@gerases
Copy link
Author

gerases commented Jan 6, 2020

Hi @rodjek, I include rspec_helper using include_relative and it's found ok. So the issue I'm seeing is only in the puppet specific methods missing.

I would be curious about the workaround because my situation is that I have a puppet repo with multiple modules, profiles, roles, etc. It's tiresome to find the closest spec directory upward of the spec file.

I'm also working on a Vim plugin (https://github.com/gerases/vim-rspec-puppet) that will run rspec on a given spec file, but because of this peculiarity, I had to add special code to find the closest spec dir.

So, I'm just curious what the solution could be. I realize it could be too much trouble.

Thank you!!

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

No branches or pull requests

2 participants