RSpec请求规范 - 缺少必需的密钥:[:id]

时间:2015-01-05 01:00:29

标签: ruby-on-rails rspec

我在运行测试时遇到错误,我无法理解。

#The error
Failure/Error: get post_path, {id: 1}
 ActionController::UrlGenerationError:
   No route matches {:action=>"show", :controller=>"posts"} missing required keys: [:id]

以下是规范。

#spec/requests/visitor_interaction_spec.rb

require 'rails_helper'

RSpec.describe 'Visitor', :type => :request do
  context 'following a post link' do
    it 'should redirect to the post' do
      get post_path, {id: 1}
      expect(response).to render_template(:show)
    end
  end
end

如果我将get post_path, {id: 1}替换为get "posts/1",则测试会按预期传递

1 个答案:

答案 0 :(得分:3)

在非控制器rspec中你用uri做,但是你需要将参数传递给路径

get post_path('1')