Reputation: 71
I'm trying to test my Post_comments#create action in the controller specs with rspec and I keep getting this error message:
Failure/Error: post :create, :post_id => post.to_param, :post_comment => attributes_for(:post_comment, comment: "New")
ArgumentError:
wrong number of arguments (2 for 0)
# ./spec/controllers/post_comments_controller_spec.rb:95:in `block (4 levels) in <top (required)>'
My Post Comments controller:
class PostCommentsController < ApplicationController
before_action :find_todo_list
def index
@post_comment = @post.post_comments.all
end
def show
@post_comment = @post.post_comments.find(params[:id])
end
def new
@post_comment = @post.post_comments.new
end
def edit
@post_comment = @post.post_comments.find(params[:id])
end
def create
@post_comment = @post.post_comments.new(post_comment_params)
if
@post_comment.save
redirect_to post_post_comments_path
flash[:success] = "Comment added successfully!"
else
flash.now[:error] = "Comment could not be saved"
render 'new'
end
end
def update
@post_comment = @post.post_comments.find(params[:id])
if
@post_comment.update(post_comment_params)
redirect_to post_post_comment_path
flash[:success] = "Comment successfully updated"
else
flash.now[:error] = "Comment could not be updated"
render 'edit'
end
end
def destroy
@post_comment = @post.post_comments.find(params[:id])
@post_comment.destroy
redirect_to post_post_comments_path
flash[:success] = "The comment was successfully deleted"
end
end
private
def find_todo_list
@post = Post.find_by(params[:post_id])
end
def post_comment_params
params.require(:post_comment).permit(:comment)
end
My controller spec that keeps failing:
describe "POST #create" do
context "flash messages" do
let(:post) {create(:post)}
it "sets flash success" do
post :create, :post_id => post.to_param, :post_comment => attributes_for(:post_comment, comment: "New")
expect(flash[:success]).to eq("Comment added successfully!")
end
end
end
I'm using factory girl so here is my factory for post comments, which has a belongs_to association with a post...duh
factory :post_comment do
comment "Post comment"
post
end
Any help would really help me out, thanks!
Upvotes: 1
Views: 1024
Reputation: 47578
let(:post) {create(:post)}
# ...
post :create
let
is a fancy way of defining a method on the current RSpec example. post
is now a method that accepts 0 arguments, thus the message wrong number of arguments (2 for 0)
.
Try naming your Post object something else.
Upvotes: 3