Is it possible to pass arguments to let block during the call?

python pass function as argument with parameters
ruby pass function as argument
python parameter passing
python pass by reference example
python pass list to function as arguments
python pass by value
ruby pass block
python pass function name as argument

I've started to use Rspec and right now I wrote several successfully worked and pretty difficult tests. But as I need more practice I did refactor of these tests few times.

I not found an answer for my question in Rspec's documentation and that's why I here.

The question is about directive let that provides an ability to return some objects by first call and not only, you know.

My current part of rspec code is:

RSpec.describe 'Users', type: :request do
  describe 'profiles' do
    context 'should be visible by' do
      it 'related company managers' do
        company = create(:company)
        sign_in create(:manager, :company, company: company) # Pay attention on this
        get user_path(create(:member, :company, company: company)) # this
        expect(response).to have_http_status 200
      end

      it 'related company owners' do
        company = create(:company)
        sign_in create(:owner, :company, company: company) # this
        get user_path(create(:member, :company, company: company)) # and this
        expect(response).to have_http_status 200
      end
    end
  end
end

There are only two example of 63 that are under the User's spec, but they are enough. I want to refactor the code to use let which will define a method with parameters, like that:

RSpec.describe 'Users', type: :request do
  let(:member) do |entity_name = :company, entity = create(entity_name)|
    create :member, entity_name, entity_name => entity
  end
  let(:manager) do |entity_name = :company, entity = create(entity_name)|
    create :manager, entity_name, entity_name => entity
  end
  let(:owner) do |entity_name = :company, entity = create(entity_name)|
    create :owner, entity_name, entity_name => entity
  end

  describe 'profiles' do
    context 'should be visible by' do
      it 'related company managers' do
        company = create(:company)
        sign_in manager(:company, company) # Become more readable here
        get user_path(member(:company, company)) # here
        expect(response).to have_http_status 200
      end

      it 'related company owners' do
        company = create(:company)
        sign_in owner(:company, company) # here
        get user_path(member(:company, company)) # and here.
        expect(response).to have_http_status 200
      end
    end
  end
end

Right after the refactor the Guard says:

  1) Users profiles should be visible by related company managers
     Failure/Error: sign_in manager(:company, company) # Become more readable here

     ArgumentError:
       wrong number of arguments (given 2, expected 0)
     # ./spec/requests/users_spec.rb:38:in `block (4 levels) in <top (required)>'

From the memorized_helpers.rb of Rspec core I saw:

def let(name, &block)
  # We have to pass the block directly to `define_method` to
  # allow it to use method constructs like `super` and `return`.
  raise "#let or #subject called without a block" if block.nil?
  raise(
    "#let or #subject called with a reserved name #initialize"
  ) if :initialize == name
  MemoizedHelpers.module_for(self).__send__(:define_method, name, &block)

  # Apply the memoization. The method has been defined in an ancestor
  # module so we can use `super` here to get the value.
  if block.arity == 1
    define_method(name) { __memoized.fetch_or_store(name) { super(RSpec.current_example, &nil) } }
  else
    define_method(name) { __memoized.fetch_or_store(name) { super(&nil) } }
  end
end

It looks like let's blocks should be defined with parameters. Or not? I haven't enough experience to determine it and I would be glad to find it out.

I think you want something like:

RSpec.describe 'Users', type: :request do
  describe 'profiles' do
    let(:request) { get user_path(user) }
    let(:user)    { create(:member, :company, company: company) }
    let(:company) { create(:company) }

    shared_examples_for 'has visibility to user' do
      before { sign_in employee }

      it do 
        request
        expect(response).to have_http_status(:success)
      end
    end

    context 'manager' do
      let(:employee) { create(:manager, company: company) }

      it_behaves_like 'has visibility to user'
    end

    context 'owner' do
      let(:employee) { create(:owner, company: company) }

      it_behaves_like 'has visibility to user'
    end
  end
end

Similarly, you should be able to do something like:

it_behaves_like 'has visibility to user' do
 let(:employee) { create(:manager, company: company) }
end

with shared examples. I suggest checking out http://www.betterspecs.org/.

(Aside, it's confusing to have a factory named :member and not :user since the GET request is for user_path; I recommend renaming it.)

Python Tutorial: Passing Arguments, If you call the function ref_demo() of the previous example - like we do in the green block further below - we can check what happens to x with the id() function: We  As a requirement for one of these functions, I needed to pass all of the parameters in a function directly to a remote computer. Here's a rough example of my function.

I've found possible solution: return a lambda and call it in place

RSpec.describe 'Users', type: :request do
  let(:member) do
    ->(entity_name = :company, entity = create(entity_name)) do
      create :member, entity_name, entity_name => entity
    end
  end
  let(:manager) do
    ->(entity_name = :company, entity = create(entity_name)) do
      create :manager, entity_name, entity_name => entity
    end
  end
  let(:owner) do
    ->(entity_name = :company, entity = create(entity_name)) do
      create :owner, entity_name, entity_name => entity
    end
  end

  describe 'profiles' do
    context 'should be visible by' do
      it 'related company managers' do
        company = create(:company)
        sign_in manager.(:company, company) # Pay attention on the dot here
        get user_path(member.(:company, company)) # here
        expect(response).to have_http_status 200
      end

      it 'related company owners' do
        company = create(:company)
        sign_in owner.(:company, company) # here
        get user_path(member.(:company, company)) # and here.
        expect(response).to have_http_status 200
      end
    end
  end
end

This is not exactly that I want, but seems like.

Python2 Tutorial: Passing Arguments, The passing of parameters and arguments in Python. First, let's have a look at the integer variables. If you call the function ref_demo() - like we do in the green block further down - we can check A function is said to have a side effect if, in addition to producing a value, it modifies the caller's environment in other ways. I'm trying figure out how to pass a statement as a method argument - to be used as part of the block filter criteria. For example, I have the following code: def method other_method.persons.each

The another solution is creation of helpers. For example, create a file in spec/support/users_helper.rb.

module UsersSpecHelper

  def member(entity = :company)
    create :member, *entity(entity)
  end

  def manager(entity = :company)
    create :manager, *entity(entity)
  end

  def owner(entity = :company)
    create :owner, *entity(entity)
  end

  private

    def entity(entity)
      if entity.is_a? Symbol
        [entity, entity => create(entity)]
      else
        name = entity.class.to_s.downcase.to_sym
        [name, name => entity]
      end
    end
end

RSpec.configure do |config|
  config.include UsersSpecHelper, type: :request
end

Uncomment in spec/rails_helper.rb the line:

Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }

and then it can be used even better:

RSpec.describe 'Users', type: :request do
  describe 'profiles' do
    context 'should be visible by' do
      it 'managers of related company' do
        company = create(:company)
        sign_in manager(company) # Changes are here
        get user_path(member(company)) # here
        expect(response).to have_http_status 200
      end

      it 'owners of related company' do
        company = create(:company)
        sign_in owner(company) # here
        get user_path(member(company)) # and here.
        expect(response).to have_http_status 200
      end
    end
  end
end

Blocks and Procs - Learn to Program, It's the ability to take a block of code (code in between do and end), wrap it up in like a method than I have shown you, because blocks can take parameters: For example, let's say there's something we want to do before and after some Our method will take some object and a proc, and will call the proc on that object. Passing an Object as argument . To pass an object as an argument we write the object name as the argument while calling the function the same way we do it for other variables. Syntax: function_name(object_name); Example: In this Example there is a class which has an integer variable ‘a’ and a function ‘add’ which takes an object as

Closures in Ruby: Blocks, Procs and Lambdas, Implicit block passing works by calling the yield keyword in a method. it can also be passed to a method just like a normal argument. In both blocks, procs and lambdas, let's zoom back out and summarize the comparison. In call-by-reference evaluation, which is also known as pass-by-reference, a function gets an implicit reference to the argument, rather than a copy of its value. As a consequence, the function can modify the argument, i.e. the value of the variable in the caller's scope can be changed.

Parameter with ampersand operator (&) in Ruby, Methods in ruby can take a block as an argument and use it inside a method. Let's create a method and define the parameter with the ampersand operator (&): Even if we explicitly pass a block, we can still call it inside the method the other programmer is likely to be more quickly oriented in your code. The code block within every function starts with a colon (:) and should be indented (space) Any arguments or input parameters should be placed within these parentheses, etc. At least one indent should be left before the code after declaring function; Same indent style should be maintained throughout the code within def function

Mastering Ruby Blocks in Less Than 5 Minutes, Well, if you call yield inside the method, then the block parameter becomes So when the block runs, it can use the parameters passed in from the original are all the rage these days so let's try to find a few interesting ways of using blocks in​  In PL/SQL, we can pass parameters to procedures and functions in three ways. 1) IN type parameter: These types of parameters are used to send values to stored procedures. 2) OUT type parameter: These types of parameters are used to get values from stored procedures.