ArgumentError: wrong number of arguments (given2, expected 0) when trying to run a test on Rspec

Asked 2 years ago, Updated 2 years ago, 114 views

I am currently creating Twitter-like applications.
Users can post and comment on the post.
This error occurs when commenting.

Error running Rspec

 1) CommentsController POST# create parameter is reasonable is registered
     Failure/Error: post: create, params: {user_id:user.id, post_id:post.id, comment:valid_attributes}, session:{}

     ArgumentError:
       wrong number of arguments (given2, expected 0)
     # ./spec/controllers/comments_controller_spec.rb:67:in`block(5 levels) in<top(required)>'
     # ./spec/controllers/comments_controller_spec.rb: 66: in `block(4 levels) in <top(required)>'

Related codes are as follows:

comments_controller_spec.rb

RSpec.describe CommentsController, type::controller do
  let(:user){
    FactoryBot.create(:admin_user)
  }

  before do
    log_in user
  end

  let(:post){
    user.posts.create(FactoryBot.attributes_for(:post))
  }

  let(:valid_attributes){
    FactoryBot.attributes_for(:comment)
  }

  describe "POST #create" do
    context "parameter is reasonable" do
      it "is registered" do
        expect{
          post:create,params:{user_id:user.id,post_id:post.id,comment:valid_attributes},session:{}
        }.to change(Comment, :count).by(1)
      end

      it "redirect post page" do
        post:create,params:{user_id:user.id,post_id:post.id,comment:valid_attributes},session:{}
        expect(response).to redirect_to post
      end
    end
 end
end

app/controllers/comments_controller.rb

def create
    @post=Post.find (params[:post_id])
    @ comment=current_user.comments.build(comment_params)
    @omment.post =@post

    [email protected]
      flash[:success] = 'success in commenting'
      redirect_to post_url (params[:post_id])
    else
      render 'new'
    end
  end

factors/comments.rb

FactoryBot.definedo
  factory:comment, class:Comment do
    content'a'*140
    user
    post 
  end
end

The debugger does not start when I set the breakpoint to comments_controller#create, so I think the error occurred before that.

We apologize for the small amount of information, but if you have experienced a similar case, please let us know.

Thank you for your cooperation.

ruby-on-rails ruby

2022-09-30 21:42

1 Answers

Welcome to Stack Overflow! I wrote let(:post), and the post in Rspec was overwritten. You can use other variables in let.


2022-09-30 21:42

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.