RSpec test controller have ArgumentError: wrong number of arguments (given 2, expected 0) Rails 4.2.8

RSpec test controller have ArgumentError: wrong number of arguments (given 2, expected 0) Rails 4.2.8

我正在尝试使用 RSpec 执行此测试代码,因为用户不会破解评论的选择:

 require 'rails_helper'

RSpec.describe ReviewsController, type: :controller do
  let(:user) { create(:user) }
  let(:state) { State.create!(name: "Hacked") }
  let(:post) { Post.create(title: "State transitions", subtitle: "Can't be hacked.", content: "State transitions now they can't be hacked anymore.", author: user) }

  context "a user without permission to set state" do
    before :each do
      assign_role!(user, :editor, post)
      sign_in user
    end

    it "cannot transition a state by passing through state_id" do
      post :create, params: { review: { text: "Did I hack it??",
                                                 state_id: state.id },
                                       post_id: post.id }
      post.reload
      expect(post.state).to be_nil
    end
  end
end

当我尝试 运行 rspec 命令时:

rspec spec/controllers/reviews_controller_spec.rb -b                 
Run options: exclude {:slow=>true}
F

Failures:

  1) ReviewsController a user without permission to set state cannot transition a state by passing through state_id
     Failure/Error:
       post :create, params: { review: { text: "Did I hack it??",
                                                  state_id: state.id },
                                        post_id: post.id }

     ArgumentError:
       wrong number of arguments (given 2, expected 0)
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/gems/rspec-core-3.6.0/lib/rspec/core/memoized_helpers.rb:298:in `block in let'
     # ./spec/controllers/reviews_controller_spec.rb:15:in `block (3 levels) in <top (required)>'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:254:in `instance_exec'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:254:in `block in run'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:500:in `block in with_around_and_singleton_context_hooks'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:457:in `block in with_around_example_hooks'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/gems/rspec-core-3.6.0/lib/rspec/core/hooks.rb:464:in `block in run'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/gems/rspec-core-3.6.0/lib/rspec/core/hooks.rb:604:in `block in run_around_example_hooks_for'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:342:in `call'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/gems/rspec-rails-3.6.0/lib/rspec/rails/example/controller_example_group.rb:191:in `block (2 levels) in <module:ControllerExampleGroup>'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:447:in `instance_exec'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:447:in `instance_exec'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/gems/rspec-core-3.6.0/lib/rspec/core/hooks.rb:375:in `execute_with'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/gems/rspec-core-3.6.0/lib/rspec/core/hooks.rb:606:in `block (2 levels) in run_around_example_hooks_for'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:342:in `call'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/gems/rspec-rails-3.6.0/lib/rspec/rails/adapters.rb:127:in `block (2 levels) in <module:MinitestLifecycleAdapter>'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:447:in `instance_exec'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:447:in `instance_exec'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/gems/rspec-core-3.6.0/lib/rspec/core/hooks.rb:375:in `execute_with'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/gems/rspec-core-3.6.0/lib/rspec/core/hooks.rb:606:in `block (2 levels) in run_around_example_hooks_for'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:342:in `call'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/gems/rspec-core-3.6.0/lib/rspec/core/hooks.rb:607:in `run_around_example_hooks_for'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/gems/rspec-core-3.6.0/lib/rspec/core/hooks.rb:464:in `run'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:457:in `with_around_example_hooks'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:500:in `with_around_and_singleton_context_hooks'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:251:in `run'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/gems/rspec-core-3.6.0/lib/rspec/core/example_group.rb:627:in `block in run_examples'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/gems/rspec-core-3.6.0/lib/rspec/core/example_group.rb:623:in `map'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/gems/rspec-core-3.6.0/lib/rspec/core/example_group.rb:623:in `run_examples'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/gems/rspec-core-3.6.0/lib/rspec/core/example_group.rb:589:in `run'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/gems/rspec-core-3.6.0/lib/rspec/core/example_group.rb:590:in `block in run'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/gems/rspec-core-3.6.0/lib/rspec/core/example_group.rb:590:in `map'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/gems/rspec-core-3.6.0/lib/rspec/core/example_group.rb:590:in `run'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/gems/rspec-core-3.6.0/lib/rspec/core/runner.rb:118:in `block (3 levels) in run_specs'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/gems/rspec-core-3.6.0/lib/rspec/core/runner.rb:118:in `map'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/gems/rspec-core-3.6.0/lib/rspec/core/runner.rb:118:in `block (2 levels) in run_specs'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/gems/rspec-core-3.6.0/lib/rspec/core/configuration.rb:1894:in `with_suite_hooks'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/gems/rspec-core-3.6.0/lib/rspec/core/runner.rb:113:in `block in run_specs'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/gems/rspec-core-3.6.0/lib/rspec/core/reporter.rb:79:in `report'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/gems/rspec-core-3.6.0/lib/rspec/core/runner.rb:112:in `run_specs'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/gems/rspec-core-3.6.0/lib/rspec/core/runner.rb:87:in `run'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/gems/rspec-core-3.6.0/lib/rspec/core/runner.rb:71:in `run'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/gems/rspec-core-3.6.0/lib/rspec/core/runner.rb:45:in `invoke'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/gems/rspec-core-3.6.0/exe/rspec:4:in `<top (required)>'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/bin/rspec:23:in `load'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/bin/rspec:23:in `<main>'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/bin/ruby_executable_hooks:15:in `eval'
     # /Users/romenigld/.rvm/gems/ruby-2.4.1@rails4.2.8/bin/ruby_executable_hooks:15:in `<main>'

Finished in 0.68874 seconds (files took 2.55 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/controllers/reviews_controller_spec.rb:14 # ReviewsController a user without permission to set state cannot transition a state by passing through state_id

我在这里寻找堆栈溢出,我注意到在 rails 5 中建议为此更改:

post post_reviews_url, params: { review: { text: "Did I hack it??", ...

创建 post 评论的路线:

rake routes | grep review
            post_reviews POST   /posts/:post_id/reviews(.:format)        reviews#create

当我 运行 rspec 命令时,我得到这个:

rspec spec/controllers/reviews_controller_spec.rb -b                 1 ↵
Run options: exclude {:slow=>true}
F

Failures:

  1) ReviewsController a user without permission to set state cannot transition a state by passing through state_id
     Failure/Error:
       post post_reviews_url, params: { review: { text: "Did I hack it??",
                                                  state_id: state.id },
                                        post_id: post.id }

     ActionController::UrlGenerationError:
       No route matches {:action=>"create", :controller=>"reviews"} missing required keys: [:post_id]

我做错了什么?

我将 post 重命名为 posting 因为它与 POST 和作品相同。

require 'rails_helper'

RSpec.describe ReviewsController, type: :controller do
  let(:user) { create(:user) }
  let(:state) { State.create!(name: "Hacked") }
  let(:posting) { Post.create(title: "State transitions", subtitle: "Can't be hacked.", content: "State transitions now they can't be hacked anymore.", author: user) }

  context "a user without permission to set state" do
    before :each do
      assign_role!(user, :editor, posting)
      sign_in user
    end

    it "cannot transition a state by passing through state_id" do
      post :create, { review: { text: "Did I hack it??",
                                                 state_id: state.id },
                                       post_id: posting.id   }
      posting.reload
      expect(posting.state).to be_nil
    end
  end
end