Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 4 如何正确使用RSpec测试PostController中的未选中复选框_Ruby On Rails 4_Capybara_Rspec Rails_Pundit - Fatal编程技术网

Ruby on rails 4 如何正确使用RSpec测试PostController中的未选中复选框

Ruby on rails 4 如何正确使用RSpec测试PostController中的未选中复选框,ruby-on-rails-4,capybara,rspec-rails,pundit,Ruby On Rails 4,Capybara,Rspec Rails,Pundit,我正在用post_controller上的RSpec测试控制器,但我很难评估没有检查类别权限的用户 期望(posting.categories)。取消选中字段(“运动”) 这项测试就像是一种让用户不破解帖子类别的方法 因此,如果用户没有权限,我将使用私有方法清理_参数以删除参数 post_controller.rb def update authorize @post, :update? @post.author = current_user if @post.upd

我正在用post_controller上的RSpec测试控制器,但我很难评估没有检查类别权限的用户

期望(posting.categories)。取消选中字段(“运动”)

这项测试就像是一种让用户不破解帖子类别的方法

因此,如果用户没有权限,我将使用私有方法清理_参数以删除参数

post_controller.rb

def update
    authorize @post, :update?
    @post.author = current_user

    if @post.update(sanitized_parameters)
      flash[:notice] = "Post has been updated."
      redirect_to @post
    else
      flash.now[:alert] = "Post has not been updated."
      render "edit"
    end
  end

...
private

  def post_params
params.require(:post).permit(:title,
                             :subtitle,
                             :content,
                             :tag_names,
                             :attachment,
                             :attachment_cache,
                             :remove_attachment,
                             :remote_attachment_url,
                             category_ids:[])
  end

  def sanitized_parameters
    whitelisted_params = post_params

    unless policy(@post).change_category?
      whitelisted_params.delete(category_ids:[])
    end

    unless policy(@post).tag?
      whitelisted_params.delete(:tag_names)
    end

    whitelisted_params
  end
错误:

Failures:

  1) PostsController restrictions for users on posts who can edit a post, but not check the categories
     Failure/Error: expect(posting.categories).to have_unchecked_field("Sports")
       expected to find field "Sports" that is not checked but there were no matches

您正在编写控制器或可能的请求测试,但Capybara仅用于功能/系统和视图测试。当您试图调用
have\u unchecked\u field(“运动”)
时,它应该会引发一个无方法错误,因为您甚至不应该将水豚方法包括到控制器测试中

由于您已将Capybara::DSL包括在所有测试中(假设),因此它将使用
posting.categories
,将其转换为字符串,将其解析为HTML,然后在其中查找元素。既然
posting.categories
不是HTML,它就永远不会这样工作。你可能想要的是

expect(posting.categories).to be_empty

我试着把这个空的。并引发此错误:Failure/error:expect(posting.categories)。要为_empty应为
#.empty?
返回true,得到false是的,我在想,此匹配是否已取消选中\u字段在控制器测试中没有意义。。。感谢您的回复@ThomasWalpole@rld如果它告诉您
posting.categories
不是空的,那么您的测试可能是错误的,或者您的代码没有按照您认为的那样执行。您可以从该错误中看到,“Sports”已添加到
posting.categories
,这正是您正在测试以确保不会发生的内容。我在尝试创建帖子时会这样做,现在我正在尝试更新帖子。更新有问题。在
之前的
块中,您正在将类别添加到
发布中
——我不确定该类别是否默认为“Sports”--如果是,那么您的测试实际上不会测试任何内容,因为该类别已被检查。
Failures:

  1) PostsController restrictions for users on posts who can edit a post, but not check the categories
     Failure/Error: expect(posting.categories).to have_unchecked_field("Sports")
       expected to find field "Sports" that is not checked but there were no matches
expect(posting.categories).to be_empty