class User < ActiveRecord::Base end RSpec.describe Kagetsu do after { User.delete_all } describe '.create_or_update' do context 'when user do not exist' do specify do expect { User.create_or_update(key: { name: 'kotoko' }, update_attributes: { email: 'kotoko@example.com' }) } .to change { User.count } .from(0) .to(1) end end context 'when user exist' do let!(:user) { User.create!(name: 'kotoko', email: 'old@example.com') } specify 'email is changed' do expect { User.create_or_update(key: { name: 'kotoko' }, update_attributes: { email: 'new@example.com' }) } .to change { user.reload.email } .from('old@example.com') .to('new@example.com') end end end end