Ruby rspec post:创建操作参数错误:未知关键字:post
这是我的FactoryBot文件post.rbRuby rspec post:创建操作参数错误:未知关键字:post,ruby,rspec,ruby-on-rails-5,factory-bot,rspec-rails,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\u controller\u spec.rb中,我有以下内容来测试post-create操作的用例 it 'creates a new customer' do p
FactoryBot.define do
factory :post do
title 'Rspec Test'
text 'test for attributes'
user_id 1
topic_id 1
end
end
在posts\u controller\u spec.rb中,我有以下内容来测试post-create操作的用例
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)>'
有人能帮我吗请求帮助者似乎不包括在内 请求规格由:type=>:Request或如果您已设置 config.inference_spec_type_from_file_location!把它们放在 规格/要求 资料来源:
代码必须是这样的
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
现在可以了我必须在参数中传递post:attributes_for(:post),比如post:create,params:{post:attributes_for(:post)}
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