Created
November 28, 2018 09:57
-
-
Save stp-che/2a09c039fbf625cee40b18712189ea36 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CreateBook | |
def initialize(title, author_guid) | |
@title, @author_guid = title, author_guid | |
end | |
def perform | |
unless author | |
log "Could not find author" | |
else | |
book.save! | |
end | |
end | |
def book | |
@book ||= Book.new title: @title, author: author | |
end | |
def author | |
return @author if defined?(@author) | |
@author = Author.find_by_guid(@author_guid) || ExternalStorage.import_author(@author_guid) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe CreateBook do | |
let(:op){ described_class.new "Lord of the Rings", "001" } | |
describe "#perform" do | |
let(:author){ create :author } | |
before { allow(op).to receive(:author){ author } } | |
it "saves the book" do | |
expect{ op.perform }.to change{ op.book.new_record? }.to(true) | |
end | |
context "when author not found" do | |
let(:author){ nil } | |
it 'does not create anything' do | |
expect{ op.perform }.to_not change{ Book.count } | |
end | |
end | |
end | |
describe '#book' do | |
let(:author){ create :author } | |
before { allow(op).to receive(:author){ author } } | |
it 'has proper attributes' do | |
expect(op.book).to have_attributes(title: "Lord of the Rings", author: author) | |
end | |
end | |
describe '#author' do | |
subject { op.author } | |
context "when author with given guid exists" do | |
let!(:author){ create :author, guid: "001" } | |
it 'returns that author' do | |
should eq author | |
end | |
end | |
context "when author does not exist" do | |
let(:author){ double :author } | |
it "imports author from ExternalAtorage" do | |
expect(ExternalStorage).to receive(:import_author).with("001"){ author } | |
should eq author | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment