Ruby on rails RSpec路由规范POST请求问题

Ruby on rails RSpec路由规范POST请求问题,ruby-on-rails,ruby,rspec,rspec-rails,rails-routing,Ruby On Rails,Ruby,Rspec,Rspec Rails,Rails Routing,我是一名RubyonRails开发人员,我使用RSpec测试了一个相当简单的Rails应用程序。我在写一些路由规范,然后我遇到了这个问题: 我的路线是这样的: Rails.application.routes.draw do root 'trip_plans#index' resources :trip_plans end require 'rails_helper' RSpec.describe 'trip_plans routes', type: :routing do de

我是一名RubyonRails开发人员,我使用RSpec测试了一个相当简单的Rails应用程序。我在写一些路由规范,然后我遇到了这个问题:

我的路线是这样的:

Rails.application.routes.draw do
  root 'trip_plans#index'
  resources :trip_plans
end
require 'rails_helper'

RSpec.describe 'trip_plans routes', type: :routing do
  describe 'creating a new plan' do
    it 'creates a new plan on post in /trip_plans' do
      expect(post: '/trip_plans').to route_to(controller: 'trip_plans', action: 'create', title: 'New Plan', day: '3')
    end
  end
end
因此,我有一个类似于
post/trip_plans
的路线,用于创建新计划并触发
trip_plans#create
操作

我的布线规范文件
spec/Routing/trip\u plans\u spec.rb
如下所示:

Rails.application.routes.draw do
  root 'trip_plans#index'
  resources :trip_plans
end
require 'rails_helper'

RSpec.describe 'trip_plans routes', type: :routing do
  describe 'creating a new plan' do
    it 'creates a new plan on post in /trip_plans' do
      expect(post: '/trip_plans').to route_to(controller: 'trip_plans', action: 'create', title: 'New Plan', day: '3')
    end
  end
end
现在我需要以某种方式将参数
标题:'New Plan',day:'3'
传递给我的
expect(post:'/trip\u plans')
,这样看起来就像是一个真正的用户正在填写表单并点击提交

如何将POST请求的参数传递给我的RSpec路由规范


提前谢谢

路由规范通常不会增加太多价值。在路由规范中,您只需测试某个路由是否与正确的控制器匹配。控制器从未被实际调用

相反,您可以使用控制器规格,用于测试应用程序如何响应用户输入:

# spec/controllers/trip_plans_controller_spec.rb
RSpec.describe TripPlansController, type: :controller do

  let(:valid_params) do
    {
        title: 'New Plan',
        day: '3'
    }
  end

  let(:invalid_params) do
    {
        day: 'xxx'
    }
  end

  describe 'POST #create' do

    let(:action) { post :create, valid_params }
    context 'with valid attributes' do
      it 'creates a new post' do
        expect { action }.to change(Post, :count).by(+1)
      end
      it 'has the correct attrributes' do
        action
        expect(assigns(:trip_plan).title).to eq 'New Plan'
        expect(assigns(:trip_plan).day).to eq 3
      end
    end

    context 'with invalid attributes' do
      let(:action) { post :create, invalid_params }

      it 'does not create a new post' do
        expect { action }.to_not change(Post, :count)
      end

      it 'renders the new template' do
        action
        expect(response).to render_template :new
      end
    end
  end
end
功能规格,它们是测试实际用户体验的端到端规格:

RSpec.feature 'Trip Plans' do
  context 'as a User' do
    scenario 'I should be able to create a trip plan' do
      visit root_path
      click_link 'Create a new trip plan'
      fill_in 'Title', with: 'Go west'
      fill_in 'Day', with: 5
      click_button 'Create trip plan'
      expect(page).to have_content 'Trip plan created.'
      expect(page).to have_content 'Go west'
    end
  end
end
控制器规格对于准确测试控制器如何响应参数以及在何处写入对数据库状态的实际期望非常有用

功能规格很好,因为它们也涵盖了您的视图,而且编写良好的规格还保证您的用户路径是可访问的。但是,它们通常不会捕获前端不明显且速度较慢的错误,因为您通常需要呈现多个页面才能获得测试的实际内容

来自功能规格的堆栈跟踪或错误消息通常不如较低级别规格有用


一个好的测试套件通常由模型规范、控制器规范和功能规范组成,这些规范涵盖了应用程序中最重要的路径。

我建议为这些类型的测试编写功能规范。IMO a功能规范是验证路由、控制器操作和屏幕流的完美方法。谢谢!我真的很感谢你的帮助。虽然有一件事:在编写功能规格(
spec/features
)时,我们使用水豚DSL(例如
feature
场景
)还是RSpec DSL(
描述
)?最佳实践是什么?
功能
场景
-。这个也不错