rspec / rspec-rails

RSpec for Rails 6+
https://rspec.info
MIT License
5.14k stars 1.03k forks source link

rspec describe/context label within loop not woking #2760

Closed chmich closed 1 month ago

chmich commented 1 month ago

Hi there

Because this feels totally strange to me i have also opened a issue on stackoverflow too.

For me this feels like a bug.

I need to have a describe/context block within a loop and I am facing a totally strange behavior at the point when I want to access a variable from outside the Rspec.describe block.

Its an app with many different user roles that need to be tested. Idea is to have a central variable with all user roles (e.g. [:staff, :company_lead, ...]) and within the request-specs I want to iterate it.

rspec-core (3.12.2, 3.12.1) rspec-rails (6.1.0, 6.0.1) rails 6.1 ruby 2.7.2

I tested the same on a newer app (ruby 3.2.2, rails 7.0.5, rspec-rails 6.0.1), but same behaviour. But, on examples like GitHub it seems that this had worked many years ago.

This works

RSpec.describe '/admin/settings', type: :request do

    ALLOWED_ROLES = %i[adm bo_adm]

    ALLOWED_ROLES.each do |role|
    describe "allowed as #{role}" do
      login("user_#{role}")
        it "check" do
          expect(1).to eq(2)
        end
    end
  end
end

runs and tests are failing like expected.

This works too

class Tests
  SUBTESTS = [:adm, :bo_adm]
end

RSpec.describe '/admin/settings', type: :request do
  Tests::SUBTESTS.each do |v|
      context "as_role_" do
        it "check" do
          expect(1).to eq(2)
        end
      end
    end
end

=> WORKS: tests are failing but i do not see which subtest is failing because the context label is always the same

This crashes

class Tests
  SUBTESTS = [:adm, :bo_adm]
end

RSpec.describe '/admin/settings', type: :request do
  Tests::SUBTESTS.each do |v|
      context "as_role_#{v}" do
        it "check" do
          expect(1).to eq(2)
        end
      end
    end
end

At the point where I include the variable v in the context label, the tests are crashing, it says "No tests were found ... All examples have been filtered out".

I do nothing different than in the first test, which works except dragging the array (Tests::SUBTESTS) from outside the Rspec.describe block where the tests reside.

Am I missing something?

Currently I have /spec/support/controller_macros.rb where all the login_as... methods are defined. There I want to put an array with all user roles and access it from all specs that are inside /spec/requests/.... But I cannot find a way to make this working.

Thanks, Chris

JonRowe commented 1 month ago

I tried to replicate this using your code snippet and it worked as expected, this could be an interaction of some kind with your setup but I don't think this is an rspec bug I'm afraid, just a sanity check because the stack overflow link you posted is wrong, but have you have named the spec file _spec.rb?

chmich commented 1 month ago

@JonRowe Thanks for the reply! The file is already named as _spec.rb. Could you please post on which versions (ruby, rails, rspec-rails) you are running this? Then i will setup a new project with exactly the same. And i am on MAC Sonoma 14.4.1.

JonRowe commented 1 month ago

Latest version in each case.

chmich commented 1 month ago

I have the feeling that this has to do with my environment, but where to search?

chris@MacMini ~/projects/Accounting $ printenv
PATH=/Users/christian/.rvm/gems/ruby-3.2.2@accounting/bin:/Users/christian/.rvm/gems/ruby-3.2.2@global/bin:/Users/christian/.rvm/rubies/ruby-3.2.2/bin:/Users/christian/.rvm/bin:/opt/homebrew/opt/postgresql@15/bin:/Users/christian/.nvm/versions/node/v21.1.0/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Library/Apple/usr/bin:/Users/christian/projects-gmbh/Accounting/node_modules/.bin:/Users/christian/Library/Application Support/JetBrains/Toolbox/scripts:/Users/christian/shell-scripts
__CFBundleIdentifier=com.jetbrains.rubymine
SHELL=/bin/zsh
TERM=xterm-256color
USER=christian
RUBY_VERSION_MANAGER_PATH=/Users/christian/.rvm/bin/rvm
FIG_JETBRAINS_SHELL_INTEGRATION=1
RUBY_VERSION_MANAGER_DISTRIBUTION_ID=ruby-3.2.2@accounting
TMPDIR=/var/folders/0b/p5dmn56s1vq_r3jj12m45rhr0000gn/T/
COMMAND_MODE=unix2003
TERMINAL_EMULATOR=JetBrains-JediTerm
SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.YHl4dQDgyj/Listeners
XPC_FLAGS=0x0
TERM_SESSION_ID=f2a9b8b9-665d-4ad0-8319-26ffebfb9916
__CF_USER_TEXT_ENCODING=0x1F5:0x0:0x0
LOGNAME=christian
LC_CTYPE=UTF-8
XPC_SERVICE_NAME=0
HOME=/Users/christian
SHLVL=1
PWD=/Users/christian/projects-gmbh/Accounting
OLDPWD=/Users/christian/projects-gmbh/Accounting
HOMEBREW_PREFIX=/opt/homebrew
HOMEBREW_CELLAR=/opt/homebrew/Cellar
HOMEBREW_REPOSITORY=/opt/homebrew
MANPATH=/Users/christian/.nvm/versions/node/v21.1.0/share/man:/opt/homebrew/share/man::
INFOPATH=/opt/homebrew/share/info:
rvm_prefix=/Users/christian
rvm_path=/Users/christian/.rvm
rvm_bin_path=/Users/christian/.rvm/bin
rvm_version=1.29.12-next (master)
GEM_HOME=/Users/christian/.rvm/gems/ruby-3.2.2@accounting
GEM_PATH=/Users/christian/.rvm/gems/ruby-3.2.2@accounting:/Users/christian/.rvm/gems/ruby-3.2.2@global
MY_RUBY_HOME=/Users/christian/.rvm/rubies/ruby-3.2.2
IRBRC=/Users/christian/.rvm/rubies/ruby-3.2.2/.irbrc
RUBY_VERSION=ruby-3.2.2
rvm_alias_expanded=
rvm_bin_flag=
rvm_docs_type=
rvm_gemstone_package_file=
rvm_gemstone_url=
rvm_niceness=
rvm_nightly_flag=
rvm_only_path_flag=
rvm_pretty_print_flag=
rvm_proxy=
rvm_quiet_flag=
rvm_ruby_bits=
rvm_ruby_file=
rvm_ruby_make=
rvm_ruby_make_install=
rvm_ruby_mode=
rvm_script_name=
rvm_sdk=
rvm_silent_flag=
rvm_use_flag=
NVM_DIR=/Users/christian/.nvm
NVM_CD_FLAGS=-q
NVM_BIN=/Users/christian/.nvm/versions/node/v21.1.0/bin
NVM_INC=/Users/christian/.nvm/versions/node/v21.1.0/include/node
DISABLE_SPRING=true
LANG=en_US.UTF-8
LC_ALL=en_US.UTF-8
EDITOR=nano
rvm_silence_path_mismatch_check_flag=1
rvm_delete_flag=0
rvm_file_name=
rvm_ruby_alias=
rvm_sticky_flag=
rvm_system_flag=
rvm_user_flag=
rvm_ruby_string=ruby-3.2.2
_=/usr/bin/printenv
chris@MacMini ~/projects/Accounting $ 
chmich commented 1 month ago

I created a issue on Jetbrains