rspec post:create action ArgumentError:unknown keyword:post

时间:2018-02-15 11:52:49

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

这是我的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

任何人都可以帮助我

2 个答案:

答案 0 :(得分:0)

似乎不包括请求助手。

  

请求规范标记为:type =&gt; :请求或如果你已经设置   config.infer_spec_type_from_file_location!将它们放入   规格/请求。

来源:

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

答案 1 :(得分:0)

代码必须像这样

 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

现在可以使用了