rspec shared examples vs shared context
shared_examples
contain a collection of examples which you can include in other describe blocks.
A shared_context
contains a collection of shared code, which you can include in your test file. Think of this like a ruby module.
You use a shared_context
in your test code by including it with the include_context
method.
On the other hand, you state that a certain thing behaves_like
a shared example group.
It's a matter of readability I guess.
UPDATE:
If you look at the source code you'll see that they're exactly the same thing. Check out line 98 in this file:
https://github.com/rspec/rspec-core/blob/master/lib/rspec/core/shared_example_group.rb#L98
alias_method :shared_context, :shared_examples
You'll also see that shared_examples_for
is another alias for the same method.
Very trivial and cosmetic, but include_context
doesn't output "behaves like" in --format documentation
.
shared_examples
are tests written in a way that you can run them in multiple settings; extracting common behavior between objects.
it_behaves_like "a correct object remover" do
...
end
shared_contexts
is any setup code that you can use to prepare a test case . This allows you to include test helper methods or prepare for the tests to run.
include_context "has many users to begin with"