Derive example description from shared context

22 views
Skip to first unread message

Jason Karns

unread,
Aug 29, 2023, 5:00:09 PM8/29/23
to rspec
We have a shared context that is included automatically by the presence of particular metadata.

I'm wondering if it's possible for the shared context to append or otherwise mutate the example's description with additional text. (Basically we want presence of the included context to be explicit in the example description itself, as well as to include pieces of the metadata itself.)

RSpec.describe Something, auth: :customer do
  it "does thing"
end

shared_context "authenticated" do
  let(:current_user) { logs_in_as self.class.metadata[:auth] }
  # hoping for a before or around hook that can insert contextual description at this scope of the example
end

config.include_context "authenticated", :auth

Desired result: "Something authenticated as a customer does thing"

Have others done something similar or have alternatives?

Thanks,
Jason Karns

Jon Rowe

unread,
Sep 7, 2023, 5:05:05 AM9/7/23
to RSpec Google Group
Hiya

Shared examples automatically wrap their examples in a context which includes contextual information, but I don’t think shared_context can do that, they’re basically more of an “include” of spec parts, you also can’t modify the description of the existing context so I think you’d need to change the strategy here, or look into modifying shared_context to have some sort of metadata the reporter uses.

Cheers
Jon
--
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 rspec+un...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages