1
votes

After doing the bundle exec rspec in the terminal window. I received two errors: [Failure/Error: send(method, file) and Syntax Error: /vagrant/src/grammable/spec/controllers/grams_controller_spec.rb:104: syntax error, unexpected keyword_end, expecting end-of-input]. I am unable to passing the test due to 1 error occurred outside of examples.

require 'rails_helper'

RSpec.describe GramsController, type: :controller 
    describe "grams#update action" do
        it "should allow users to successfully update grams" do
            gram = FactoryBot.create(:gram, message: "Initial Value")
            patch :update, params: { id: gram.id, gram: { message: 'Changed' } }
            expect(response).to redirect_to root_path
            gram.reload
            expect(gram.message).to eq "Changed"
        end

        it "should have http 404 error if the gram cannot be found" do
            patch :update, params: { id: "YOLOSWAG", gram: { message: 'Changed' } }
            expect(response).to have_http_status(:not_found)
        end

        it "should render the edit form with an http status of unprocessable_entity" do
            gram = FactoryBot.create(:gram, message: "Initial Value")
            patch :update, params: { id: gram.id, gram: { message: '' } }
            expect(response).to have_http_status(:unprocessable_entity)
            gram.reload
            expect(gram.message).to eq "Initial Value"
        end
    end

    describe "grams#edit action" do
        it "should successfully show the edit form if the gram is found" do
            gram = FactoryBot.create(:gram)
            get :edit, params: { id: gram.id }
            expect(response).to have_http_status(:success)
        end

        it "should return a 404 error message if the gram is not found" do
            get :edit, params: { id: 'SWAG' }
            expect(response).to have_http_status(:not_found)
        end
    end

    describe "grams#show action" do
        it "should successfully show the page if the gram is found" do
            gram = FactoryBot.create(:gram)
            get :show, params: { id: gram.id }
            expect(response).to have_http_status(:success)
        end

        it "should return a 404 error if the gram is not found" do
            get :show, params: { id: 'TACOCAT' }
            expect(response).to have_http_status(:not_found)
        end
    end

    describe "grams#index action" do
        it "should successfully show the page" do
            get :index
            expect(response).to have_http_status(:success)
        end 
    end

    describe "grams#new action" do

        it "should require users to be logged in" do
            get :new
            expect(response).to redirect_to new_user_session_path
        end

        it "should successfully show the new form" do
            user = FactoryBot.create(:user)
            sign_in user

            get :new
            expect(response).to have_http_status(:success)
        end
    end

    describe "grams#create action" do

        it "should require users to be logged in" do
            post :create, params: { gram: { message: "Hello" } }
            expect(response).to redirect_to new_user_session_path
        end

        it "should successfully create a new gram in our database" do
            user = FactoryBot.create(:user)
            sign_in user

            post :create, params: { gram: { message: 'Hello!' } }
            expect(response).to redirect_to root_path

            gram = Gram.last
            expect(gram.message).to eq("Hello!")
            expect(gram.user).to eq(user)
        end

        it "should properly deal with validation errors" do
            user = FactoryBot.create(:user)
            sign_in user

            post :create, params: { gram: { message: '' } }
            expect(response).to have_http_status(:unprocessable_entity)
            expect(Gram.count).to eq Gram.count
        end
    end
end
1
I do not understand the Syntax Error and Failure/Error on my part. I thought I did it correct. - Brolin
You are having an extra end in the code in after first two test cases. Can you edit the question and add the entire file. - Prasath Rajasekaran
My apologize, I am new to this coding and new on here as well. I hope I edited the question and code correct? - Brolin

1 Answers

0
votes

I think you have missed do in the first line RSpec.describe GramsController, type: :controller. Find below the edited code. Hope this helps!

require 'rails_helper'

RSpec.describe GramsController, type: :controller do
    describe "grams#update action" do
        it "should allow users to successfully update grams" do
            gram = FactoryBot.create(:gram, message: "Initial Value")
            patch :update, params: { id: gram.id, gram: { message: 'Changed' } }
            expect(response).to redirect_to root_path
            gram.reload
            expect(gram.message).to eq "Changed"
        end

        it "should have http 404 error if the gram cannot be found" do
            patch :update, params: { id: "YOLOSWAG", gram: { message: 'Changed' } }
            expect(response).to have_http_status(:not_found)
        end

        it "should render the edit form with an http status of unprocessable_entity" do
            gram = FactoryBot.create(:gram, message: "Initial Value")
            patch :update, params: { id: gram.id, gram: { message: '' } }
            expect(response).to have_http_status(:unprocessable_entity)
            gram.reload
            expect(gram.message).to eq "Initial Value"
        end
    end

    describe "grams#edit action" do
        it "should successfully show the edit form if the gram is found" do
            gram = FactoryBot.create(:gram)
            get :edit, params: { id: gram.id }
            expect(response).to have_http_status(:success)
        end

        it "should return a 404 error message if the gram is not found" do
            get :edit, params: { id: 'SWAG' }
            expect(response).to have_http_status(:not_found)
        end
    end

    describe "grams#show action" do
        it "should successfully show the page if the gram is found" do
            gram = FactoryBot.create(:gram)
            get :show, params: { id: gram.id }
            expect(response).to have_http_status(:success)
        end

        it "should return a 404 error if the gram is not found" do
            get :show, params: { id: 'TACOCAT' }
            expect(response).to have_http_status(:not_found)
        end
    end

    describe "grams#index action" do
        it "should successfully show the page" do
            get :index
            expect(response).to have_http_status(:success)
        end 
    end

    describe "grams#new action" do

        it "should require users to be logged in" do
            get :new
            expect(response).to redirect_to new_user_session_path
        end

        it "should successfully show the new form" do
            user = FactoryBot.create(:user)
            sign_in user

            get :new
            expect(response).to have_http_status(:success)
        end
    end

    describe "grams#create action" do

        it "should require users to be logged in" do
            post :create, params: { gram: { message: "Hello" } }
            expect(response).to redirect_to new_user_session_path
        end

        it "should successfully create a new gram in our database" do
            user = FactoryBot.create(:user)
            sign_in user

            post :create, params: { gram: { message: 'Hello!' } }
            expect(response).to redirect_to root_path

            gram = Gram.last
            expect(gram.message).to eq("Hello!")
            expect(gram.user).to eq(user)
        end

        it "should properly deal with validation errors" do
            user = FactoryBot.create(:user)
            sign_in user

            post :create, params: { gram: { message: '' } }
            expect(response).to have_http_status(:unprocessable_entity)
            expect(Gram.count).to eq Gram.count
        end
    end
end