<commit_before>require 'rails_helper'

RSpec.describe QuestionsController, type: :controller do
  let(:user)     { create(:user) }
  let(:quiz)     { create(:quiz) }
  let(:question) { create(:question) }

  describe "#new" do
    context "user signed in" do
      before { log_in(user) }
      before { get :new, quiz_id: quiz.id, question: attributes_for(:question) }

      it "renders the new template" do
        expect(response).to render_template(:new)
      end

      it "set a instance variable to equal quiz" do
        expect(assigns(:quiz)).to eq(quiz)
      end

      it "set a instance variable to a new question" do
        expect(assigns(:question)).to be_a_new Question
      end
    end

    context "user not signed in" do
      before { get :new, quiz_id: quiz.id, question: attributes_for(:question) }

      it "redirects to sign in page" do
        expect(response).to redirect_to login_path
      end
    end
  end

  describe "#create" do
    context "user not signed in" do
      before { post :create, quiz_id: quiz.id, question: attributes_for(:question) }

      it "redirects to sign in page" do
        expect(response).to redirect_to login_path
      end
    end

    context "user signed in" do
      before { log_in(user) }

      def valid_request
        post :create, quiz_id: quiz.id, question: attributes_for(:question)
      end

      it "set a instance variable to equal quiz" do
        valid_request
        expect(assigns(:quiz)).to eq(quiz)
      end

      it "creates a new quiz in the database" do
        expect { valid_request }.to change { Question.count }.by(1)
      end

      it "sets a flash message" do
        valid_request
        expect(flash[:notice]).to be
      end

      it "redirects to quiz questions path" do
        valid_request
        expect(response).to redirect_to(quiz_questions_path)
      end
    end
  end
end<commit_msg>rspec tests for question#create, invalid request<commit_after>require 'rails_helper'

RSpec.describe QuestionsController, type: :controller do
  let(:user)     { create(:user) }
  let(:quiz)     { create(:quiz) }
  let(:question) { create(:question) }

  describe "#new" do
    context "user signed in" do
      before { log_in(user) }
      before { get :new, quiz_id: quiz.id, question: attributes_for(:question) }

      it "renders the new template" do
        expect(response).to render_template(:new)
      end

      it "set a instance variable to equal quiz" do
        expect(assigns(:quiz)).to eq(quiz)
      end

      it "set a instance variable to a new question" do
        expect(assigns(:question)).to be_a_new Question
      end
    end

    context "user not signed in" do
      before { get :new, quiz_id: quiz.id, question: attributes_for(:question) }

      it "redirects to sign in page" do
        expect(response).to redirect_to login_path
      end
    end
  end

  describe "#create" do
    context "user not signed in" do
      before { post :create, quiz_id: quiz.id, question: attributes_for(:question) }

      it "redirects to sign in page" do
        expect(response).to redirect_to login_path
      end
    end

    context "user signed in" do
      before { log_in(user) }

      context "user signed in with valid request" do
        def valid_request
          post :create, quiz_id: quiz.id, question: attributes_for(:question)
        end

        it "set a instance variable to equal quiz" do
          valid_request
          expect(assigns(:quiz)).to eq(quiz)
        end

        it "creates a new quiz in the database" do
          expect { valid_request }.to change { Question.count }.by(1)
        end

        it "sets a flash message" do
          valid_request
          expect(flash[:notice]).to be
        end

        it "redirects to quiz questions path" do
          valid_request
          expect(response).to redirect_to(quiz_questions_path)
        end
      end

      context "with invalid request" do
        def invalid_request
          post :create, quiz_id: quiz.id, question: {title: nil}
        end

        it "doesn't create a record in the database" do
          expect { invalid_request }.to change { Question.count }.by(0)
        end

        it "renders the new template" do
          invalid_request
          expect(response).to render_template(:new)
        end

        it "sets a flash message" do
          invalid_request
          expect(flash[:alert]).to be
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.08502566391430484, 'fraction_numerical': 0.0006694934166480696, 'mean_word_length': 2.3126385809312637, 'pattern_counts': {'":': 0, '<': 3, '<?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': '1360803', 'n_tokens_mistral': 1345, 'n_tokens_neox': 1278, 'n_words': 443}