Fredo Santa
Fredo Santa

Reputation: 3

Load file error on Rspec

I'm following along with a tutorial on testing with Rspec. Getting a syntax error when trying to run the test. Here's my test code:

require 'rails_helper'

RSpec.describe CommentsController, type: :controller do
  describe "comments#create action" do
    it "should allow admins to create comments on posts" do

        post = FactoryGirl.create(:post)

        admin = FactoryGirl.create(:admin)
        sign_in admin

        post :create, params: { post_id: post.id, comment: { message: 'awesome post' } }

        expect(response).to redirect_to root_path
        expect(post.comments.length).to eq 1
        expect(post.comments.first.message).to eq "awesome gram"

    end

    it "should require an admin to be logged in to comment on a post" do
        post = FactoryGirl.create(:post)
        post :create, params: { post_id: post.id, comment: { message: 'awesome post' } }
        expect(response).to redirect_to new_admin_session_path  
    end

    it "should return http status code of not found if the post isn't found" do
        admin = FactoryGirl.create(:admin)
        sign_in admin
        post :create, params: { post_id: 'SUNSHINE', comment: { message: 'awesome post'} }
        expect(response).to have_http_status :not_found
    end
  end
end

Here's the controller:

class CommentsController < ApplicationController
  before_action :authenticate_admin!, only: [:create]

  def create
      @post = Post.find_by_id(params[:post_id])
      return render_not_found if @post.blank?

      @post.comments.create(comment_params.merge(admin: current_admin))
      redirect_to root_path
  end

  private

  def render_not_found(status=:not_found)
      render plain: "#{status.to_s.titleize} :(", status: status
  end

  def comment_params
      params.require(:comment).permit(:message)
  end
end

Here's the terminal output when running the test:

Terminal output

What's odd is that when I comment the lines that are producing the error, the tests run as intended with the last test passing. I've checked the test file along with similar posts that describe the same problem and it looks to me like the syntax is correct. New to Rails so pardon the rookie mistakes.

Upvotes: 0

Views: 402

Answers (2)

treiff
treiff

Reputation: 1306

I think it has to do with you defining a variable named post then trying to call the Rspec method post:

it "should require an admin to be logged in to comment on a post" do
  post = FactoryGirl.create(:post)
  post :create, params: { post_id: post.id, comment: { message: 'awesome post' } }
  expect(response).to redirect_to new_admin_session_path  
end

Try re-naming it: to call the Rspec method post:

it "should require an admin to be logged in to comment on a post" do
  message = FactoryGirl.create(:post)
  post :create, params: { post_id: message.id, comment: { message: 'awesome post' } }
  expect(response).to redirect_to new_admin_session_path  
end

Upvotes: 0

Mate Solymosi
Mate Solymosi

Reputation: 5967

The problem is that you have a variable named post:

post = FactoryGirl.create(:post)   # define `post` variable
post :create, params: ...          # try to call `post` method

Therefore, on subsequent lines post will refer to the variable, not the post method.

Solution 1: Rename the variable

my_post = FactoryGirl.create(:post)
post :create, params: { post_id: my_post.id, ... }

Solution 2: Use self.post to access the method

post = FactoryGirl.create(:post)
self.post :create, params: { post_id: post.id, ... }

Replicating the issue in irb:

def foo
  "bar"
end

foo = "wat"
#=> "wat"

foo :hello
# SyntaxError: (irb):63: syntax error, unexpected ':', expecting end-of-input
# foo :hello
#      ^

Upvotes: 1

Related Questions