ruby-on-rails rspec-rails后面的什么逻辑生成非REST描述,如'POST /create'

bweufnob  于 2023-02-17  发布在  Ruby
关注(0)|答案(1)|浏览(130)

bounty将在8小时后过期。回答此问题可获得+50的声誉奖励。petRUShka正在寻找来自声誉良好来源的答案

rspec-railsrequest specs提供了scaffold生成器,生成如下代码:

RSpec.describe "/widgets", type: :request do
  # ...
  describe "POST /create" do
    context "with valid parameters" do
      it "creates a new Widget" do
        expect {
          post widgets_url, params: { widget: valid_attributes }

rspec -f d输出如下

/widgets
  POST /create
    with valid parameters
      creates a new Widget

因此,POST /widgets/create这样的路由实际上不存在,而POST /widgets存在。
这种描述背后的计划是什么?这种方法有正式的定义吗?
我能提供的唯一解释是方案是Model —> HTTP verb —> /CRUD action,但似乎没有太大意义。

xlpyo6sf

xlpyo6sf1#

So here the type of RSpec test is request so 
  post => http verb
  widgets_url => is request_path or request_url of create method
  params => you pass attributes for post http method

If you use type of RSpec is controller then you have to write this
 You need to use method name instead of request_path or request_url

  RSpec.describe WidgetsController, type: :controller do
    describe "POST #create " do
     context "with valid parameters" do
      it "creates a new Widget" do
        expect {
          post :create, params: { widget: valid_attributes }
        }.to eq("") 
      end
     end
    end
  end

相关问题