require 'rails_helper'

describe LinkApiResponsePresenter do
  describe '#present' do
    let(:authority) { build(:district_council) }
    let(:presenter) { described_class.new(authority, link) }

    context 'link is present' do
      let(:link) { build(:link) }
      let(:expected_response) do
        {
          "local_authority" => {
            "name" => authority.name,
            "snac" => authority.snac,
            "tier" => 'district',
            "homepage_url" => authority.homepage_url
          },
          "local_interaction" => {
            "lgsl_code" => link.service.lgsl_code,
            "lgil_code" => link.interaction.lgil_code,
            "url" => link.url
          }
        }
      end

      it 'returns combined details for the local authority and local interaction' do
        expect(presenter.present).to eq(expected_response)
      end
    end

    context 'no link is present' do
      let(:link) { nil }
      let(:expected_response) do
        {
          "local_authority" => {
            "name" => authority.name,
            "snac" => authority.snac,
            "tier" => 'district',
            "homepage_url" => authority.homepage_url
          }
        }
      end

      it 'returns details for just the local authority' do
        expect(presenter.present).to eq(expected_response)
      end
    end
  end
end

require 'rails_helper'

describe LinkApiResponsePresenter do
  describe '#present' do
    let(:authority) { build(:district_council) }
    let(:presenter) { described_class.new(authority, link) }

    context 'link is present' do
      let(:link) { create(:link) }
      let(:expected_response) do
        {
          "local_authority" => {
            "name" => authority.name,
            "snac" => authority.snac,
            "tier" => 'district',
            "homepage_url" => authority.homepage_url
          },
          "local_interaction" => {
            "lgsl_code" => link.service.lgsl_code,
            "lgil_code" => link.interaction.lgil_code,
            "url" => link.url
          }
        }
      end

      it 'returns combined details for the local authority and local interaction' do
        expect(presenter.present).to eq(expected_response)
      end
    end

    context 'no link is present' do
      let(:link) { nil }
      let(:expected_response) do
        {
          "local_authority" => {
            "name" => authority.name,
            "snac" => authority.snac,
            "tier" => 'district',
            "homepage_url" => authority.homepage_url
          }
        }
      end

      it 'returns details for just the local authority' do
        expect(presenter.present).to eq(expected_response)
      end
    end
  end
end

Fix test by creating link associations
Fix test by creating link associations

The test was failing with
```
Failures:

  1) LinkApiResponsePresenter#present link is present returns combined details for the local authority and local interaction
     Failure/Error: "lgsl_code" => @link.service.lgsl_code,

     NoMethodError:
       undefined method `lgsl_code' for nil:NilClass
     # ./app/presenters/link_api_response_presenter.rb:31:in `link_details'
     # ./app/presenters/link_api_response_presenter.rb:9:in `present'
     # ./spec/presenters/link_api_response_presenter_spec.rb:27:in `block (4 levels) in <top (required)>'

```

This is because when building the `link`, its associations are ignored.
Creating (instead of building) the `link` solves this problem because it
also creates its associations that were previously not present.
