Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.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 测试对Posts控制器的POST请求_Ruby On Rails_Rspec - Fatal编程技术网

Ruby on rails 测试对Posts控制器的POST请求

Ruby on rails 测试对Posts控制器的POST请求,ruby-on-rails,rspec,Ruby On Rails,Rspec,我试图测试未经授权的用户向我的Rails 4博客应用程序的Posts控制器发出的直接POST请求的结果。在Rails教程之后,我已经为Users控制器实现了如下功能测试: describe 'attempting to issue a direct POST request while not signed in' do before { post users_path } specify { expect(response).to redirect_to signin_path

我试图测试未经授权的用户向我的Rails 4博客应用程序的Posts控制器发出的直接POST请求的结果。在Rails教程之后,我已经为Users控制器实现了如下功能测试:

describe 'attempting to issue a direct POST request while not signed in' do
    before { post users_path }
    specify { expect(response).to redirect_to signin_path }
end
describe 'in the Posts controller' do
  let(:post) { Post.create(...) }
  .
  .
  .
  describe 'attempting to issue a direct POST request while not signed in' do
    before { post posts_path } # 'post' is interpreted to be the variable!
    specify { expect(response).to redirect_to signin_path }
  end
end
但是,在
before
块中,尝试在Posts控制器上执行相同的测试失败:

describe 'attempting to issue a direct POST request while not signed in' do
    before { post posts_path }
    specify { expect(response).to redirect_to signin_path }
end

ArgumentError: wrong number of arguments (1 for 0)
等效测试包含
修补post_路径(post)
删除post_路径(post)
功能,并通过控制器中的before_操作

我的路线:

       posts GET    /posts(.:format)                       posts#index
             POST   /posts(.:format)                       posts#create
    new_post GET    /posts/new(.:format)                   posts#new
   edit_post GET    /posts/:id/edit(.:format)              posts#edit
        post GET    /posts/:id(.:format)                   posts#show
             PATCH  /posts/:id(.:format)                   posts#update
             PUT    /posts/:id(.:format)                   posts#update
             DELETE /posts/:id(.:format)                   posts#destroy

RSpec是否被POST/POST(即请求的名称与控制器的名称)弄糊涂了?

好吧,方法和模型名称之间确实存在混淆,尽管我没有向上游看得足够远:我的测试设置如下:

describe 'attempting to issue a direct POST request while not signed in' do
    before { post users_path }
    specify { expect(response).to redirect_to signin_path }
end
describe 'in the Posts controller' do
  let(:post) { Post.create(...) }
  .
  .
  .
  describe 'attempting to issue a direct POST request while not signed in' do
    before { post posts_path } # 'post' is interpreted to be the variable!
    specify { expect(response).to redirect_to signin_path }
  end
end
因此,HTTP方法POST和在测试块开头声明的变量“POST”之间存在命名冲突。将变量重命名为“test_post”修复了所有问题


@DaveNewton:显然,在POST请求被彻底拒绝的情况下,可以在没有参数的情况下对其进行测试。

这可能会混淆,因为创建POST需要参数。@DaveNewton:我理解这一点,但完全相同的测试适用于用户控制器,在Rails教程中也适用于Microposts控制器(清单10.23)。用户和POST模型(以及Hartl的MicroPost)都验证了几个参数的存在,因此,如果问题如您所建议的那样,发送一个空POST请求对其中任何一个都不起作用。伙计,谢谢!我已经绞尽脑汁想了好几个小时了。