rspec post:create action ArgumentError: unknown keyword: post

rspec post:create action ArgumentError: unknown keyword: post

这是我的 FactoryBot 文件 post.rb

FactoryBot.define do
  factory :post do
    title 'Rspec Test'
    text  'test for attributes'
    user_id 1
    topic_id 1
  end
end

在 posts_controller_spec.rb 我有以下测试用例 post 创建操作

it 'creates a new customer' do
  post :create, post: attributes_for(:post)
  expect(Post.count).not_to eq(0)
end

我收到以下错误

$stdout.sync=true;$stderr.sync=true;load([=12=]=ARGV.shift) /Users/vignesh/assignment/bin/rails spec
/Users/vignesh/.rvm/rubies/ruby-2.4.1/bin/ruby -I/Users/vignesh/.rvm/gems/ruby-2.4.1/gems/rspec-core-3.7.1/lib:/Users/vignesh/.rvm/gems/ruby-2.4.1/gems/rspec-support-3.7.1/lib /Users/vignesh/.rvm/gems/ruby-2.4.1/gems/rspec-core-3.7.1/exe/rspec --pattern spec/\*\*\{,/\*/\*\*\}/\*_spec.rb

ArgumentError: unknown keyword: post
/Users/vignesh/.rvm/gems/ruby-2.4.1/gems/rails-controller-testing-1.0.2/lib/rails/controller/testing/template_assertions.rb:61:in `process'
/Users/vignesh/.rvm/gems/ruby-2.4.1/gems/devise-4.4.1/lib/devise/test/controller_helpers.rb:35:in `block in process'
/Users/vignesh/.rvm/gems/ruby-2.4.1/gems/devise-4.4.1/lib/devise/test/controller_helpers.rb:102:in `catch'
/Users/vignesh/.rvm/gems/ruby-2.4.1/gems/devise-4.4.1/lib/devise/test/controller_helpers.rb:102:in `_catch_warden'
/Users/vignesh/.rvm/gems/ruby-2.4.1/gems/devise-4.4.1/lib/devise/test/controller_helpers.rb:35:in `process'
/Users/vignesh/.rvm/gems/ruby-2.4.1/gems/rails-controller-testing-1.0.2/lib/rails/controller/testing/integration.rb:12:in `block (2 levels) in <module:Integration>'
./spec/controllers/posts_controller_spec.rb:25:in `block (3 levels) in <top (required)>'

我正在使用 Rails 5.1.4

ruby 4.4.1

factory bot 4.0
rspec 3.4

谁能帮帮我

似乎不​​包括请求助手。

Request specs are marked by :type => :request or if you have set config.infer_spec_type_from_file_location! by placing them in spec/requests.

来源:

https://relishapp.com/rspec/rspec-rails/docs/request-specs/request-spec

代码必须像这样

 describe 'POST create' do
    it 'creates a new Post' do
      expect {
        post :create, params: {topic_id: @topic, post: @post_attributes}
      }.to change(Post, :count).by(1)
    end

现在可以了

我在 rspec 3.xxx 的教程中也遇到了相同类型的错误,尽管我使用的是 rspec 5.xxx 所以这是我认为可行的解决方案

it 'creates a new customer' do
  expect{ 
    post :create, params: { topic_id: @topic, post: attributes_for(:post)}
        }.to change(Post, :count).by(1)
end

有点类似我的 code.My 代码完美运行。希望你的也能奏效

 describe 'Post #create' do
      before(:each) do
      end
      context 'with valid attributes' do
        it 'saves the new product in the database' do
          expect{
            post :create, params: { product: attributes_for(:product)}
          }.to change(Product, :count).by(1)
        end
      end
    end