Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rspec shared examples vs shared context

What is the real difference between shared_examples and shared_context ?

My observations :

  1. I can test same things using both (i.e. with shared_examples or shared_context)

  2. But some of my other tests fails if I use later one.

Observation #1 :

I compared shared_examples and shared_context per documentation on https://www.relishapp.com/

Syntactical differences are :

  • shared_context to define a block that will be evaluated in the context of example groups by implicitly matching metadata

Example :

shared_context "shared stuff", :a => :b do   ... end 
  • The way these are included or called from a test file

shared_examples

include_examples "name"      # include the examples in the current context it_behaves_like "name"       # include the examples in a nested context it_should_behave_like "name" # include the examples in a nested context 

shared_context

include_context "shared stuff" 

Observation #2

I have a test case

shared_context 'limit_articles' do |factory_name|   before do     @account = create(:account)   end    it 'should restrict 3rd article' do     create_list(factory_name, 3, account: @account)      article4 = build(factory_name, account: @account)     article4.should be_invalid   end    it 'should allow 1st article' do     ...   end    it 'should allow 2nd article' do     ...   end end 

And include the context in a spec file which already has one shared_context included, then the existing one fails. But I change the order then all my test passes

Fails

include_context 'existing_shared_context'  include_context 'limit_articles' 

Also if I replace the shared_context with shared_examples and accordingly include it in test case.

Passes

include_context 'existing_shared_context'  it_behaves_like 'limit_articles' 
like image 245
swapab Avatar asked Jan 14 '14 15:01

swapab


People also ask

What is shared context in RSpec?

Shared Contexts in RSpec In RSpec, every example runs in a context: data and configuration for the example to draw on, including lifecycle hooks, let and subject declarations, and helper methods. These contexts are inherited (and can be overridden) by nested example groups.

What is shared context?

Shared context occurs when people or people and machines collaboratively and interactively solve problems. Shared context contains knowledge about the problem at hand and background and common-sense knowledge.

What is context in RSpec?

According to the rspec source code, “context” is just a alias method of “describe”, meaning that there is no functional difference between these two methods. However, there is a contextual difference that'll help to make your tests more understandable by using both of them.

What is Include_context?

include_context . When implicitly including shared contexts via matching metadata, the normal way is to define matching metadata on an example group, in which case the context is included in the entire group. However, you also have the option to include it in an individual example instead.


2 Answers

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" 
like image 148
rafroehlich2 Avatar answered Sep 19 '22 18:09

rafroehlich2


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.

like image 45
Jon Avatar answered Sep 18 '22 18:09

Jon