DEV Community

n350071🇯🇵
n350071🇯🇵

Posted on

[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

Top comments (0)