ruby-on-rails - POST :create rspec controller testing error, 参数 2 为 0 的错误数量

标签 ruby-on-rails ruby rspec controller

我正在尝试使用 rspec 在 Controller 规范中测试我的 Post_comments#create 操作,但我不断收到此错误消息:

 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)>'

我的帖子评论 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

我的 Controller 规范一直在失败:

 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

结束

我正在使用 factory girl,所以这是我的评论工厂,它与帖子有 belongs_to 关联...呃

  factory :post_comment do
    comment "Post comment"
    post
  end

任何帮助都会帮助我,谢谢!

最佳答案

let(:post) {create(:post)}
# ...
post :create

let 是在当前 RSpec 示例中定义方法的一种奇特方式。 post 现在是一个接受 0 个参数的方法,因此消息 wrong number of arguments (2 for 0)

尝试将您的 Post 对象命名为其他名称。

关于ruby-on-rails - POST :create rspec controller testing error, 参数 2 为 0 的错误数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37263130/

相关文章:

ruby-on-rails - 限制用户在相关模型的 Controller 中的访问

ruby-on-rails - Rspec - 未初始化的常量 RAILS_ENV

ruby-on-rails - 在 Rspec 中的 Controller 规范中测试子域

ruby-on-rails - FactoryGirl 关联模型故障 : "SystemStackError: stack level too deep"

sql - 仅返回此 ActiveRecord 查询中的唯一记录

ruby-on-rails - 用散列对数组求和

ruby-on-rails - gemfile 中包含的 gem 仍然需要类文件中的 "include"?

ruby - apn_on_rails 的哪个分支更适合 rails3?

ruby-on-rails - 在外键上添加索引如何工作?

ruby-on-rails - Amazon S3,如何处理从上传到对象可用性的延迟