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

Is it possible to access instance variables from custom placeholders #236

Open
Tyflomate opened this issue Feb 16, 2021 · 5 comments
Open

Comments

@Tyflomate
Copy link

Hello ! I have a problem that is mentioned in #62 but this issue is pretty old so I decided to create a new one.

I would like to access an instance variable inside a place holder to do for example something like this:

placeholder :right do
    match /admin/ do
      @user.rights.find_by(admin: true)
    end

    match /visitor/ do
      @user.rights.find_by(visitor: true)
    end

    match /member/ do
      @user.rights.find_by(member: true)
    end
  end

@user is initialized in an other step file. For now, I tried implementing my steps outside a module but then @user was nil in my placeholder so I tried to move everything into the same module to counter this but now I have the error: undefined method placeholder' for #Module:0x00007fa2f6ad3c98::Steps`.

So my question is: is it possible to access instance variables into placeholders ? How could I manage to make this work ?

Thanks a lot guys !

@Tyflomate Tyflomate changed the title Is it possible to access instance variables from placeholders Is it possible to access instance variables from custom placeholders Feb 16, 2021
@Tyflomate
Copy link
Author

@gongo, maybe you can help me out ? Thank you !

@nitishr
Copy link

nitishr commented Feb 28, 2021

I've been able to place a placeholder inside a module by including extend Turnip::DSL in the module. That might make the instance variable accessible in the placeholder as well, although I haven't tried that myself.

@Tyflomate
Copy link
Author

Nice thanks for the comment ! I'll try that 👍

@Tyflomate
Copy link
Author

Tyflomate commented Mar 3, 2021

Well i can now indeed put a custom placeholder into a module but instance variables are still nil inside it...

@gongo
Copy link
Collaborator

gongo commented May 13, 2021

Hi @Tyflomate. Sorry for the late reply.

is it possible to access instance variables into placeholders ?

It impossible because placeholders are defined outside each step (module).

How could I manage to make this work ?

1️⃣ Declare with a global variable:

placeholder :right do
  match /admin/ do
    $user.rights.find_by(admin: true)
  end
  
  match /visitor/ do
    $user.rights.find_by(visitor: true)
  end
  
  match /member/ do
    $user.rights.find_by(member: true)
  end
end

2️⃣ Use steps_for

module Right
  step 'set user' do
    # examples..
    @user = {
      admin: 'admin',
      visitor: 'visitor',
      member: 'member',
    }
  end

  step 'displayed :name' do |name|
    @right.should eq(name)
  end
end

steps_for :admin do
  include Right

  step 'get right' do
    @right = @user[:admin]
  end
end

steps_for :visitor do
  include Right

  step 'get right' do
    @right = @user[:visitor]
  end
end
Feature: Steps for a feature
  Background:
    Given set user

  @admin
  Scenario: Admin
    Given get right
    Then displayed "admin"

  @visitor
  Scenario: Visitor
    Given get right
    Then displayed "visitor"

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

No branches or pull requests

3 participants