I admit that I don't fully understand the example you provided. I'm not
sure how it translates into RSpec code, nor what the connection is to
"standard Ruby scoping".
Anyhow, what I got out of this thread is that "accessing shared example
block variables in a defined method is not possible". I'll use strategies
such as exposing the variable in a let variable or passing the values as
method parameters. Thank you all for the help.
On Wednesday, June 24, 2020 at 7:16:37 AM UTC-4, Jon Rowe wrote:
>
> This is standard Ruby scoping (as I’ve pointed out on stack overflow).
> Phil’s answer is a great workaround but I strongly encourage not shadowing
> the name (e.g. call it something else) to prevent confusion around
> precedence.
>
> I elaborate further on the Stack Overflow post, but shall repeat here.
>
> The parameter passed to the block is available within the block's closure.
> So the evaluated string `"parameter=#{parameter}"` being within the closure
> works just fine.
>
> What you're trying to do the same as is this:
>
> ```
> b = "Hi!"
>
> def a
> puts b
> end
>
> a()
> # NameError (undefined local variable or method `b' for main:Object)
> ```
>
> As Phil points out a solution is to wrap parameter in a `let`, which is
> the same (roughly) as doing:
>
> ```
> b = "Hi!"
>
> def a
> puts b
> end
>
> define_method(:b) { b }
>
> a()
> # Hi!
> # => nil
> ```
>
> Cheers
> Jon Rowe
> ---------------------------
> [email protected] <javascript:>
> jonrowe.co.uk
>
> On 22 June 2020 at 21:04, Evan Brodie wrote:
>
> I also asked on Stackoverflow (
> 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/
>
> ***********************************
>
> 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):
>
> 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
>
> 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].
To view this discussion on the web visit
https://groups.google.com/d/msgid/rspec/e7150083-491a-49de-9041-c540b6d1b1b3o%40googlegroups.com.