Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'spec_helper'
- module Users
- describe NameService do
- describe '#name' do
- let(:user) { User.new }
- # I don't like using subject {}, so I name the subject following the
- # described class name, so when you read the example bellow, you know
- # directly what kind is that object. I hate also using the described_class helper
- # it makes changing the specs easier later on when the class changes (which it doesn't
- # happen very frequentely), so you only change it in one place. But it makes
- # reading examples very hard, and you need to go back and forth between
- # the example and the `describe Class`.
- let(:name_service){ NameService.new(user) }
- # this is shared between all the specs, for DRYing code.
- before do
- allow(user).to receive(:posts?).and_return(has_posts)
- end
- # use context instead of "if statements"
- context 'WHEN user does NOT HAVE posts' do
- # this makes it clear what this context block is all about
- let(:has_posts) { false }
- it 'returns user.name' do
- expect(name_service.name).to eq(user.name)
- end
- end
- context 'WHEN user HAS posts' do
- let(:has_posts) { true }
- it 'returns "#{user.name} (has posts)"' do
- expect(name_service.name).to eq("#{user.name} (has posts)")
- end
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement