DEV Community

n350071πŸ‡―πŸ‡΅
n350071πŸ‡―πŸ‡΅

Posted on

2 3

[RSpec] shared_context

simple code sample

subject { word }

shared_context 'hoge' do
  let(:word) { 'hoge' }
end
shared_context 'fuga' do
  let(:word) { 'fuga' }
end

context 'hoge' do
  include_context 'hoge'
  it { is_expected.to eq 'hoge' }
end
Enter fullscreen mode Exit fullscreen mode

merit

Assume that you have like this many contexts, and the let() is not short.

In this way, we can concentlate to see what we test, instead to read a lot of code.

shared_context 'people_happy' do
  # looong!
end

context 'dog' do
  let(:animal) {creat(:dog)}
  context 'people_happy' do
    include_context 'people_happy'
    # test
  end
end
context 'cat' do
  let(:animal) {creat(:cat)}
  context 'people_happy' do
    include_context 'people_happy'
    # test
  end
end
context 'horse' do
  let(:animal) {creat(:hourse)}
  context 'people_happy' do
    include_context 'people_happy'
    # test
  end
end
Enter fullscreen mode Exit fullscreen mode

πŸ”— Parent Note

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay