<commit_before>require 'spec_helper'

describe AnswersController do
  let(:answer) { create(:answer) }
  let(:user) { create(:user) }

  describe 'POST create' do
    let(:post_call) do
      -> { post(:create,
               question_id: answer.question.id,
               answer: { description: answer.description })
      }
    end

    before { sign_in(answer.user) }

    context "around create" do
      it { expect{ post_call.call }.to change{ Answer.count }.by(1) }
    end

    context "after create" do
      before { post_call.call }

      it { expect(response).to redirect_to(answer.question) }
      it { expect(flash[:notice]).to eql(I18n.t('answer.flash.created')) }
    end
  end

  describe 'GET vote' do
    context "with a logged user" do
      before { sign_in(user) }

      describe 'vote up' do
        before { get(:vote, question_id: answer.question.id, id: answer.id, vote: 'up') }

        it { expect(response).to redirect_to(answer.question) }
        it { expect(flash[:notice]).to eql(I18n.t('vote.flash.voted'))}
      end

      describe 'vote down' do
        before { get(:vote, question_id: answer.question.id, id: answer.id, vote: 'down') }

        it { expect(response).to redirect_to(answer.question) }
        it { expect(flash[:notice]).to eql(I18n.t('vote.flash.voted'))}
      end
    end

    context "with the answer owner as the logged user" do
      before { sign_in(answer.user) }

      describe 'vote up' do
        let(:vote_call) do
          -> { get(:vote, question_id: answer.question.id, id: answer.id, vote: 'up') }
        end

        it { expect{ vote_call.call rescue  nil }.to_not change{ answer.up_votes } }
        it { expect{ vote_call.call }.to raise_error(CanCan::AccessDenied) }
      end

      describe 'vote down' do
        let(:vote_call) do
          -> { get(:vote, question_id: answer.question.id, id: answer.id, vote: 'down') }
        end

        it { expect{ vote_call.call rescue  nil }.to_not change{ answer.down_votes } }
        it { expect{ vote_call.call }.to raise_error(CanCan::AccessDenied) }
      end
    end
  end

  describe 'GET accept' do
    let(:accept_call) { ->{ get(:accept, question_id: answer.question.id, id: answer.id) } }

    context 'the logged user is the question owner' do
      before do
        sign_in(answer.question.user)
        accept_call.call
      end

      it { expect(response).to redirect_to(answer.question) }
      it { expect(flash[:notice]).to eq(I18n.t('answer.flash.answered')) }
    end

    context 'the logged user is not the question owner' do
      before { sign_in(user) }

      it { expect{ accept_call.call }.to raise_error(CanCan::AccessDenied) }
    end
  end
end<commit_msg>Use subject instead of let to store the call proc<commit_after>require 'spec_helper'

describe AnswersController do
  let(:answer) { create(:answer) }
  let(:user) { create(:user) }

  describe 'POST create' do
    subject do
      -> { post(:create,
             question_id: answer.question.id,
                answer: { description: answer.description }) }
    end

    before { sign_in(answer.user) }

    context "around create" do
      it { expect{ subject.call }.to change{ Answer.count }.by(1) }
    end

    context "after create" do
      before { subject.call }

      it { expect(response).to redirect_to(answer.question) }
      it { expect(flash[:notice]).to eql(I18n.t('answer.flash.created')) }
    end
  end

  describe 'GET vote' do
    context "with a logged user" do
      before { sign_in(user) }

      describe 'vote up' do
        before { get(:vote, question_id: answer.question.id, id: answer.id, vote: 'up') }

        it { expect(response).to redirect_to(answer.question) }
        it { expect(flash[:notice]).to eql(I18n.t('vote.flash.voted'))}
      end

      describe 'vote down' do
        before { get(:vote, question_id: answer.question.id, id: answer.id, vote: 'down') }

        it { expect(response).to redirect_to(answer.question) }
        it { expect(flash[:notice]).to eql(I18n.t('vote.flash.voted'))}
      end
    end

    context "with the answer owner as the logged user" do
      before { sign_in(answer.user) }

      describe 'vote up' do
        subject do
          -> { get(:vote, question_id: answer.question.id, id: answer.id, vote: 'up') }
        end

        it { expect{ subject.call rescue  nil }.to_not change{ answer.up_votes } }
        it { expect{ subject.call }.to raise_error(CanCan::AccessDenied) }
      end

      describe 'vote down' do
        subject do
          -> { get(:vote, question_id: answer.question.id, id: answer.id, vote: 'down') }
        end

        it { expect{ subject.call rescue  nil }.to_not change{ answer.down_votes } }
        it { expect{ subject.call }.to raise_error(CanCan::AccessDenied) }
      end
    end
  end

  describe 'GET accept' do
    subject { ->{ get(:accept, question_id: answer.question.id, id: answer.id) } }

    context 'the logged user is the question owner' do
      before do
        sign_in(answer.question.user)
        subject.call
      end

      it { expect(response).to redirect_to(answer.question) }
      it { expect(flash[:notice]).to eq(I18n.t('answer.flash.answered')) }
    end

    context 'the logged user is not the question owner' do
      before { sign_in(user) }

      it { expect{ subject.call }.to raise_error(CanCan::AccessDenied) }
    end
  end
end
{'fraction_non_alphanumeric': 0.12562349898392758, 'fraction_numerical': 0.003325327914280436, 'mean_word_length': 2.540876389797253, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '1073960', 'n_tokens_mistral': 1747, 'n_tokens_neox': 1689, 'n_words': 452}