On Wednesday, July 2, 2014 12:31:42 PM UTC-7, Arup Rakshit wrote: > > Hi, > > I am having hard time to understand what are the situations I need to > think > about shared_context instead of shared_examples. From the doco of > shared_examples > https://relishapp.com/rspec/rspec-core/v/3-0/docs/example-groups/shared-examples. > > > > shared_context doco is not much clear about its actual use-cases. Can > anyone > highlight me telling the use-case wise difference between these 2 ? > > -- > ================ > Regards, > Arup Rakshit > ================ > Debugging is twice as hard as writing the code in the first place. > Therefore, > if you write the code as cleverly as possible, you are, by definition, not > smart enough to debug it. > > --Brian Kernighan >
Implementation wise, they are actually identical: both declare a named block of example group code that can later be included in any other example group. In fact, they are simply aliases: https://github.com/rspec/rspec-core/blob/2c0fb2f6b2f2389bec86815fa1279b0622468ba8/lib/rspec/core/shared_example_group.rb#L50-L61 Where the difference comes in is in how you include them: `it_behaves_like` (and `it_should_behave_like`, an alias) defines a nested example group, and then eval's the shared group block in that context, where as `include_context` or `include_examples`, eval the block directly in the including context. Generally for shared examples you want the former, but for context sharing you'll want the latter, as the point is explicitly to share the context stuff (before hooks, helper methods, etc). As for when to use one vs the other: use `shared_examples` when you've got some common examples you want to share. Use `shared_context` when you've got some common context stuff you want to share, such as helper methods, before hooks, etc. HTH, Myron -- 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 post to this group, send email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/6d83a0d8-f9fa-4f91-9ed4-691152bde75d%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
