borisano
borisano

Reputation: 1386

How do I pass variable from let to shared example?

I have a test like this:

describe 'event creation' do
  let(:user) { create :user }
  ...
  it_behaves_like 'also creates public_activity record', user
end

I need user record to check if public_activity was created correctly, but example above is not working, since variables from let are only accessible from example context.

I made this workaround:

describe 'event creation' do
  let(:user) { create :user }
  ...

  describe 'public activity' do
    before { @user = user }
    it_behaves_like 'also creates public_activity record', @user
  end
end

It works, but I don't like this approach very much. Is there any better way to accomplish this?

Upvotes: 3

Views: 777

Answers (1)

M. Stavnycha
M. Stavnycha

Reputation: 433

Try using include_examples instead of it_behaves_like. Then test cases will use current context.

Upvotes: 1

Related Questions