My understanding (and someone else may be able to clarify or correct this) is 
that you should think of the code inside the if as an anonymous function to 
which the “|parameter|” is passed. Therefore, the scope of “parameter” is the 
“it” block, not the “do” block (as you would think by looking at the code).

The workaround for this situation is to pass the parameter into 
“print_the_parameter”, in which case you’ll have a local copy to work with.

I’ve run into similar issues with putting code outside the “it” and having it 
behave in unexpected ways.


> On Jun 22, 2020, at 1:02 PM, Evan Brodie <[email protected]> wrote:
> 
> Also asked on Stackoverflow 
> (https://stackoverflow.com/questions/62522543/rspec-shared-example-access-parameter-in-a-helper-method
>  
> <https://stackoverflow.com/questions/62522543/rspec-shared-example-access-parameter-in-a-helper-method>),
>  but I'm posting here too because this is the preferred location to ask for 
> rspec help according to your website: http://rspec.info/help/ 
> <http://rspec.info/help/>
> 
> shared_examples "some example group" do |parameter|
>   it "does something" do
>     puts "parameter=#{parameter}"
> 
> 
>     print_the_parameter
>   end
> 
> 
>   def print_the_parameter
>     puts "parameter=#{parameter}"
>   end
> end
> Viewed 3 times
> 
> 0
> 
> 
> Suppose I create an Rspec shared example group with one parameter (the 
> business purpose of the tests are irrelevant, it is an overly simplified 
> version of my current codebase):
> 
>   it "does something" do
>     puts "parameter=#{parameter}"
> 
>     print_the_parameter
>   end
> 
>   def print_the_parameter
>     puts "parameter=#{parameter}"
>   end
> 
> I am able to access the parameter as a variable just fine with the it test 
> block. However, I am running into an "undefined local variable or method" 
> when I try to access parameter from a method. Why is that? I have proven in 
> my codebase (and is prevalently shown in Rspec documentation) that the 
> parameter is available in test blocks, lifecycle methods like before, and in 
> let variable declarations. But why not helper methods?
> 
> Thank you.
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "rspec" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected] 
> <mailto:[email protected]>.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/rspec/e3758c6e-df22-40ad-8e7f-6bb50559b1f1o%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/rspec/e3758c6e-df22-40ad-8e7f-6bb50559b1f1o%40googlegroups.com?utm_medium=email&utm_source=footer>.

-- 
You received this message because you are subscribed to the Google Groups 
"rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rspec/086623A6-2322-4463-86CF-EB9F778A027A%40pobox.com.

Reply via email to