I’m using decorator objects in a Rails app so that I don’t have to do
nil? checks in views. Everything is working nicely and it’s easy to test, but I’m suspicious that my test:code ratio is a little high. Am I being too verbose? Is there a better way this could be done?
# spec/decorators/client_decorator_spec.rb require_relative "../../app/decorators/client_decorator" describe ClientDecorator do describe ".position" do context "with a recorded client position" do it "returns the name of the position" do occupation = double("Occupation", name: "CEO") client = double("Client", occupation: occupation) expect(ClientDecorator.new(client).position).to eq "CEO" end end context "with no recorded position" do it "returns an empty string" do client = double("Client", occupation: nil) expect(ClientDecorator.new(client).position).to eq "" end end end end
# app/decorators/client_decorator.rb require "simple_decorator" class ClientDecorator < SimpleDecorator def position return "" unless occupation occupation.name end end