【问题标题】:RSpec test controller have ArgumentError: wrong number of arguments (given 2, expected 0) Rails 4.2.8RSpec 测试控制器有 ArgumentError: wrong number of arguments (given 2, expected 0) Rails 4.2.8
【发布时间】:2018-04-04 13:43:00
【问题描述】:

我正在尝试使用 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??", ...

创建帖子评论的路线:

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]

我做错了什么?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 rspec-rails controllers


    【解决方案1】:

    我将帖子重命名为发布,因为它与 POST 和 work 的相同。

    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
    

    【讨论】:

    • 非常感谢。我已经搜索了几个小时的代码,但找不到错误。从let!(:post) { create(:post) }let!(:posting) { create(:post) } 的简单更改最终使测试变为绿色。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-01
    • 1970-01-01
    • 2015-08-19
    • 1970-01-01
    • 2013-12-12
    相关资源
    最近更新 更多