繁体   English   中英

控制器规格中的参数数目错误(给定2,预期为0)

[英]wrong number of arguments (given 2, expected 0) in controller spec

我想检查创建动作是否通过。

 describe "#create" do
  let!(:user){ create(:user) }
  let!(:post){ create_list(:post, 3, user: user) }
    context "authenticated user" do
      it "adds a new post" do   
        post_params = FactoryBot.attributes_for(:post)
        sign_in user  
        expect{ post :create, params: {post: post_params} }.to change(user.posts, :count).by(1)
      end
    end
  end

但我的错误是

Failure/Error: expect{ post :create, params: {post: post_params} }.to change(user.posts, :count).by(1)

     ArgumentError:
       wrong number of arguments (given 2, expected 0)

posts_controller:

def create
    @post = Post.new(post_params)
    if params[:images]
      if @post.save
        params[:images].each do |img|
          @post.photos.create(image: img)
        end
      else
      end
      redirect_to posts_path
      flash[:notice] = "success"
    else
      redirect_to posts_path
      flash[:alert] = "failure"
    end
  end

这很可能是因为您正在使用的变量let!(:post)post :create, params: {post: post_params}post方法)冲突。

解决方案是将let!(:post)更改为let!(:posts)因为它仍然是列表。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM