【问题标题】:rspec post:create action ArgumentError: unknown keyword: postrspec post:create action ArgumentError: unknown keyword: post
【发布时间】:2018-07-26 04:09:40
【问题描述】:

这是我的 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 中,我有以下内容来测试创建后操作的用例

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($0=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

谁能帮帮我

【问题讨论】:

    标签: ruby rspec ruby-on-rails-5 factory-bot rspec-rails


    【解决方案1】:

    虽然我使用的是 rspec 5.xxx,但我在 rspec 3.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
    

    这和我的代码有点相似。我的代码运行良好。希望你的也能工作

     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
    

    【讨论】:

      【解决方案2】:

      代码必须是这样的

       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
      

      现在可以了

      【讨论】:

        【解决方案3】:

        似乎不包括请求助手。

        请求规范由 :type => :request 标记,或者如果您已设置 config.infer_spec_type_from_file_location!通过将它们放入 规格/要求。

        来源:

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

        【讨论】:

        • 我必须在参数中传递帖子:attributes_for(:post),就像这个帖子:create, params:{post: attributes_for(:post)}
        猜你喜欢
        • 1970-01-01
        • 2017-01-03
        • 2014-11-07
        • 2021-06-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-24
        相关资源
        最近更新 更多