无法正确运行rspec

时间:2018-08-01 03:05:44

标签: ruby-on-rails rspec

rspec版本:3.5 rails版本:5.2

控制器:

def show
  # Remove all source.
end

测试控制器:

describe 'GET #show' do
  it 'returns not found' do
    get :show, params: {id: post}
    expect(response).to have_http_status(:not_found)
  end
end

此测试用例将测试404状态,但出现500个内部服务器错误:

expected the response to have status code :not_found (404) but it was :internal_server_error (500)

使用--backtrace进行测试时:

$ rspec spec/controller/posts_controller_spec.rb --backtrace

可以发现此行出现错误:

 Failure/Error: expect(response).to have_http_status(:not_found)
   expected the response to have status code :not_found (404) but it was :internal_server_error (500)
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-support-3.7.1/lib/rspec/support.rb:97:in `block in <module:Support>'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-support-3.7.1/lib/rspec/support.rb:106:in `notify_failure'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-expectations-3.7.0/lib/rspec/expectations/fail_with.rb:35:in `fail_with'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-expectations-3.7.0/lib/rspec/expectations/handler.rb:40:in `handle_failure'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-expectations-3.7.0/lib/rspec/expectations/handler.rb:50:in `block in handle_matcher'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-expectations-3.7.0/lib/rspec/expectations/handler.rb:27:in `with_matcher'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-expectations-3.7.0/lib/rspec/expectations/handler.rb:48:in `handle_matcher'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/given_core-3.8.0/lib/given/rspec/monkey.rb:21:in `handle_matcher'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-expectations-3.7.0/lib/rspec/expectations/expectation_target.rb:65:in `to'
 # ./spec/controllers/posts_controller_spec.rb:8:in `block (4 levels) in <top (required)>'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.7.1/lib/rspec/core/example.rb:254:in `instance_exec'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.7.1/lib/rspec/core/example.rb:254:in `block in run'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.7.1/lib/rspec/core/example.rb:500:in `block in with_around_and_singleton_context_hooks'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.7.1/lib/rspec/core/example.rb:457:in `block in with_around_example_hooks'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.7.1/lib/rspec/core/hooks.rb:466:in `block in run'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.7.1/lib/rspec/core/hooks.rb:606:in `block in run_around_example_hooks_for'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.7.1/lib/rspec/core/example.rb:342:in `call'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-rails-3.7.2/lib/rspec/rails/example/controller_example_group.rb:191:in `block (2 levels) in <module:ControllerExampleGroup>'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.7.1/lib/rspec/core/example.rb:447:in `instance_exec'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.7.1/lib/rspec/core/example.rb:447:in `instance_exec'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.7.1/lib/rspec/core/hooks.rb:375:in `execute_with'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.7.1/lib/rspec/core/hooks.rb:608:in `block (2 levels) in run_around_example_hooks_for'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.7.1/lib/rspec/core/example.rb:342:in `call'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-rails-3.7.2/lib/rspec/rails/adapters.rb:127:in `block (2 levels) in <module:MinitestLifecycleAdapter>'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.7.1/lib/rspec/core/example.rb:447:in `instance_exec'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.7.1/lib/rspec/core/example.rb:447:in `instance_exec'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.7.1/lib/rspec/core/hooks.rb:375:in `execute_with'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.7.1/lib/rspec/core/hooks.rb:608:in `block (2 levels) in run_around_example_hooks_for'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.7.1/lib/rspec/core/example.rb:342:in `call'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.7.1/lib/rspec/core/hooks.rb:609:in `run_around_example_hooks_for'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.7.1/lib/rspec/core/hooks.rb:466:in `run'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.7.1/lib/rspec/core/example.rb:457:in `with_around_example_hooks'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.7.1/lib/rspec/core/example.rb:500:in `with_around_and_singleton_context_hooks'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.7.1/lib/rspec/core/example.rb:251:in `run'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.7.1/lib/rspec/core/example_group.rb:628:in `block in run_examples'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.7.1/lib/rspec/core/example_group.rb:624:in `map'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.7.1/lib/rspec/core/example_group.rb:624:in `run_examples'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.7.1/lib/rspec/core/example_group.rb:590:in `run'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.7.1/lib/rspec/core/example_group.rb:591:in `block in run'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.7.1/lib/rspec/core/example_group.rb:591:in `map'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.7.1/lib/rspec/core/example_group.rb:591:in `run'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.7.1/lib/rspec/core/example_group.rb:591:in `block in run'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.7.1/lib/rspec/core/example_group.rb:591:in `map'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.7.1/lib/rspec/core/example_group.rb:591:in `run'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.7.1/lib/rspec/core/runner.rb:118:in `block (3 levels) in run_specs'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.7.1/lib/rspec/core/runner.rb:118:in `map'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.7.1/lib/rspec/core/runner.rb:118:in `block (2 levels) in run_specs'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.7.1/lib/rspec/core/configuration.rb:1926:in `with_suite_hooks'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.7.1/lib/rspec/core/runner.rb:113:in `block in run_specs'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.7.1/lib/rspec/core/reporter.rb:79:in `report'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.7.1/lib/rspec/core/runner.rb:112:in `run_specs'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.7.1/lib/rspec/core/runner.rb:87:in `run'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.7.1/lib/rspec/core/runner.rb:71:in `run'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.7.1/lib/rspec/core/runner.rb:45:in `invoke'
 # /Users/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.7.1/exe/rspec:4:in `<top (required)>'
 # /Users/user/.rbenv/versions/2.3.3/bin/rspec:23:in `load'
 # /Users/user/.rbenv/versions/2.3.3/bin/rspec:23:in `<main>'

那么rspec-railsrails之间的版本冲突吗?

1 个答案:

答案 0 :(得分:2)

在您的代码中

def show
  # Remove all source.
end

此方法中没有渲染或重定向代码,因此它将尝试渲染隐式视图show.html.erb。但是,如果您没有定义该视图,它将返回500(内部服务器错误)

如果您只想显示未找到,请给

def show
  head :not_found
end