Maybe that's simple, but it confuses me. I have such a code now:
describe VarnishLogExaminer::Entry do
let(:entry) do
file_string = File.read('./spec/assets/varnish_example.xml')
first_entry_string = file_string.split("\n").first
described_class.new(first_entry_string)
end
describe 'initialize' do
context 'when a proper Varnish log line is given' do
it 'saves remote host properly' do
expect(entry.remote_host_ip).to eql '85.164.152.30'
end
# ...
end
end
describe 'to_h' do
it 'returns hash with all entry properties' do
expect(entry.to_h).to eql(
# ...
)
end
end
# ... other methods using the entry instance
end
It seems ugly, because the initialization of the entry
object happens in the let
block, not in the context. The alternative could be initializing it once again in the context or making a method running in the context and before each of other methods, but in my opinion both of the options violate the DRY rule. What solution would be the best from the point of view of code readability and flexibility?