如何编写Rspec HTTP方法以打入嵌套的show view?

时间:2018-08-15 16:35:19

标签: ruby-on-rails rspec

我有一条这样的铁路路线:

get 'posts/:category/:permalink',  to: 'blog/posts#show',  as: :post

Rake路线告诉我有关上述路线的信息:

post GET    /posts/:category/:permalink(.:format)     blog/posts#show

我只是想编写正确的RSpec http方法来测试它是否呈现了显示模板。

require 'spec_helper'

module Blog
  RSpec.describe PostsController, type: :controller do
    describe '#show' do
      it 'renders show template' do
        get :show, {category: 'tech', permalink: 'great-post'}
        expect(response).to render_template('show')
      end
    end
  end
end

然而,此rspec测试遇到以下错误:

F

Failures:

  1) Blog::PostsController#show renders show template
     Failure/Error: get :show, {category: 'tech', permalink: 'great-post'}

     ArgumentError:
       unknown keywords: category, permalink
     # ./spec/controllers/blog/posts/show_spec.rb:7:in `block (3 levels) in <module:Blog>'

有人知道如何正确地在rspec中绘制此方法以到达Blog :: PostsController#show吗?

get :show, {category: 'tech', permalink: 'great-post'}

1 个答案:

答案 0 :(得分:1)

您需要将categorypermalink指定为params

module Blog
  RSpec.describe PostsController, type: :controller do
    describe '#show' do
      it 'renders show template' do
        get :show, params: { category: 'tech', permalink: 'great-post' }
        expect(response).to render_template('show')
      end
    end
  end
end