<commit_before>require 'rails_helper'

RSpec.describe StartGame do
  let(:player1) { Player.new }
  let(:player2) { Player.new }
  let(:game)    { Game.new }
  let(:service) { StartGame.new(game) }

  context "when called" do
    context "and game not #ready?" do
      before { game.players << player1 }

      it "bails" do
        expect(service.call).to be false
      end

      it "gives errors" do
        expect { service.call }.to change { service.errors.size }.by(1)
      end
    end

    context "and game is #ready?" do
      before do
        game.players << player1
        game.players << player2
      end

      it "runs" do
        expect(service.call).to be true
      end

      it "has no errors" do
        expect { service.call }
          .to_not change { service.errors.size }
      end

      it "is no longer #pending" do
        expect { service.call }
          .to change { game.pending }
          .from(true).to(false)
      end
    end

    context "more than once" do
      let(:second_service) { StartGame.new(game) }

      before do
        game.players << player1
        game.players << player2
        expect(service.call).to be true
      end

      it "bails" do
        expect(second_service.call).to be false
      end

      it "gives errors" do
        expect { service.call }.to change { service.errors.size }.by(1)
      end
    end
  end
end
<commit_msg>Refactor: Pull test common behaviour up into shared_examples<commit_after>require 'rails_helper'

RSpec.shared_examples "a service with errors" do
  context "when called" do
    it "bails" do
      expect(service.call).to be false
    end

    it "gives errors" do
      expect { service.call }.to change { service.errors.size }.by(1)
    end
  end
end

RSpec.describe StartGame do
  let(:player1) { Player.new }
  let(:player2) { Player.new }
  let(:game)    { Game.new }
  let(:service) { StartGame.new(game) }

  context "when called" do
    context "before players have joined" do
      it_behaves_like "a service with errors"
    end

    context "and game not #ready?" do
      before { game.players << player1 }
      it_behaves_like "a service with errors"
    end

    context "more than once" do
      before do
        game.players << player1
        game.players << player2
        second_service = StartGame.new(game)
        expect(second_service.call).to be true
      end

      it_behaves_like "a service with errors"
    end

    context "and game is #ready?" do
      before do
        game.players << player1
        game.players << player2
      end

      it "runs" do
        expect(service.call).to be true
      end

      it "has no errors" do
        expect { service.call }
          .to_not change { service.errors.size }
      end

      it "is no longer #pending" do
        expect { service.call }
          .to change { game.pending }
          .from(true).to(false)
      end

      it "deals cards to players" do
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.0936241610738255, 'fraction_numerical': 0.005704697986577181, 'mean_word_length': 1.9869739478957915, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2992873', 'n_tokens_mistral': 944, 'n_tokens_neox': 913, 'n_words': 286}