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

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

🔗 Parent Note

Top comments (0)

Timeless DEV post...

How to write a kickass README

Arguably the single most important piece of documentation for any open source project is the README. A good README not only informs people what the project does and who it is for but also how they use and contribute to it.

If you write a README without sufficient explanation of what your project does or how people can use it then it pretty much defeats the purpose of being open source as other developers are less likely to engage with or contribute towards it.