Тестовый контроллер RSpec имеет ArgumentError: неверное количество аргументов (задано 2, ожидается 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/[email protected]/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/[email protected]/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:254:in `instance_exec'
     # /Users/romenigld/.rvm/gems/[email protected]/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:254:in `block in run'
     # /Users/romenigld/.rvm/gems/[email protected]/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/[email protected]/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:457:in `block in with_around_example_hooks'
     # /Users/romenigld/.rvm/gems/[email protected]/gems/rspec-core-3.6.0/lib/rspec/core/hooks.rb:464:in `block in run'
     # /Users/romenigld/.rvm/gems/[email protected]/gems/rspec-core-3.6.0/lib/rspec/core/hooks.rb:604:in `block in run_around_example_hooks_for'
     # /Users/romenigld/.rvm/gems/[email protected]/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:342:in `call'
     # /Users/romenigld/.rvm/gems/[email protected]/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/[email protected]/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:447:in `instance_exec'
     # /Users/romenigld/.rvm/gems/[email protected]/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:447:in `instance_exec'
     # /Users/romenigld/.rvm/gems/[email protected]/gems/rspec-core-3.6.0/lib/rspec/core/hooks.rb:375:in `execute_with'
     # /Users/romenigld/.rvm/gems/[email protected]/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/[email protected]/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:342:in `call'
     # /Users/romenigld/.rvm/gems/[email protected]/gems/rspec-rails-3.6.0/lib/rspec/rails/adapters.rb:127:in `block (2 levels) in <module:MinitestLifecycleAdapter>'
     # /Users/romenigld/.rvm/gems/[email protected]/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:447:in `instance_exec'
     # /Users/romenigld/.rvm/gems/[email protected]/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:447:in `instance_exec'
     # /Users/romenigld/.rvm/gems/[email protected]/gems/rspec-core-3.6.0/lib/rspec/core/hooks.rb:375:in `execute_with'
     # /Users/romenigld/.rvm/gems/[email protected]/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/[email protected]/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:342:in `call'
     # /Users/romenigld/.rvm/gems/[email protected]/gems/rspec-core-3.6.0/lib/rspec/core/hooks.rb:607:in `run_around_example_hooks_for'
     # /Users/romenigld/.rvm/gems/[email protected]/gems/rspec-core-3.6.0/lib/rspec/core/hooks.rb:464:in `run'
     # /Users/romenigld/.rvm/gems/[email protected]/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:457:in `with_around_example_hooks'
     # /Users/romenigld/.rvm/gems/[email protected]/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:500:in `with_around_and_singleton_context_hooks'
     # /Users/romenigld/.rvm/gems/[email protected]/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:251:in `run'
     # /Users/romenigld/.rvm/gems/[email protected]/gems/rspec-core-3.6.0/lib/rspec/core/example_group.rb:627:in `block in run_examples'
     # /Users/romenigld/.rvm/gems/[email protected]/gems/rspec-core-3.6.0/lib/rspec/core/example_group.rb:623:in `map'
     # /Users/romenigld/.rvm/gems/[email protected]/gems/rspec-core-3.6.0/lib/rspec/core/example_group.rb:623:in `run_examples'
     # /Users/romenigld/.rvm/gems/[email protected]/gems/rspec-core-3.6.0/lib/rspec/core/example_group.rb:589:in `run'
     # /Users/romenigld/.rvm/gems/[email protected]/gems/rspec-core-3.6.0/lib/rspec/core/example_group.rb:590:in `block in run'
     # /Users/romenigld/.rvm/gems/[email protected]/gems/rspec-core-3.6.0/lib/rspec/core/example_group.rb:590:in `map'
     # /Users/romenigld/.rvm/gems/[email protected]/gems/rspec-core-3.6.0/lib/rspec/core/example_group.rb:590:in `run'
     # /Users/romenigld/.rvm/gems/[email protected]/gems/rspec-core-3.6.0/lib/rspec/core/runner.rb:118:in `block (3 levels) in run_specs'
     # /Users/romenigld/.rvm/gems/[email protected]/gems/rspec-core-3.6.0/lib/rspec/core/runner.rb:118:in `map'
     # /Users/romenigld/.rvm/gems/[email protected]/gems/rspec-core-3.6.0/lib/rspec/core/runner.rb:118:in `block (2 levels) in run_specs'
     # /Users/romenigld/.rvm/gems/[email protected]/gems/rspec-core-3.6.0/lib/rspec/core/configuration.rb:1894:in `with_suite_hooks'
     # /Users/romenigld/.rvm/gems/[email protected]/gems/rspec-core-3.6.0/lib/rspec/core/runner.rb:113:in `block in run_specs'
     # /Users/romenigld/.rvm/gems/[email protected]/gems/rspec-core-3.6.0/lib/rspec/core/reporter.rb:79:in `report'
     # /Users/romenigld/.rvm/gems/[email protected]/gems/rspec-core-3.6.0/lib/rspec/core/runner.rb:112:in `run_specs'
     # /Users/romenigld/.rvm/gems/[email protected]/gems/rspec-core-3.6.0/lib/rspec/core/runner.rb:87:in `run'
     # /Users/romenigld/.rvm/gems/[email protected]/gems/rspec-core-3.6.0/lib/rspec/core/runner.rb:71:in `run'
     # /Users/romenigld/.rvm/gems/[email protected]/gems/rspec-core-3.6.0/lib/rspec/core/runner.rb:45:in `invoke'
     # /Users/romenigld/.rvm/gems/[email protected]/gems/rspec-core-3.6.0/exe/rspec:4:in `<top (required)>'
     # /Users/romenigld/.rvm/gems/[email protected]/bin/rspec:23:in `load'
     # /Users/romenigld/.rvm/gems/[email protected]/bin/rspec:23:in `<main>'
     # /Users/romenigld/.rvm/gems/[email protected]/bin/ruby_executable_hooks:15:in `eval'
     # /Users/romenigld/.rvm/gems/[email protected]/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??", ...

маршруты для создания пост-обзора:

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]

Что я делаю плохо?

1 ответ

Я переименовываю сообщение для публикации, потому что оно совпадает с 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
Другие вопросы по тегам