0
我試圖創建一個rspec路由到嵌套的資源,但我不斷收到No route matches
錯誤。路由到嵌套的導軌路由
1) V1::DevicesController scan submitted to #create
Failure/Error: post "/v1/devices/#{device.serial_number}/scans.json", params: { data: scan }
ActionController::UrlGenerationError:
No route matches {:action=>"/v1/devices/7929e287466c493ba03947c189cadc81/scans.json", :controller=>"v1/devices", :data=>#<Rack::Test::UploadedFile:0x00556f7c423808 @content_type="application/gzip", @original_filename="sample_test.gz", @tempfile=#<Tempfile:/tmp/sample_test.gz20170719-65-sdfczl>>}
我的天賦,現在是非常簡單的:
需要 'rails_helper'
RSpec.describe V1::DevicesController, type: :controller do
it "scan submitted to #create" do
company = Company.create(name: "Test Company", domain: "testcompany.com")
device = Device.create(name: "Test Device", company_id: company.id)
scan = fixture_file_upload("#{Rails.root}/spec/assets/sample_test.gz", 'application/gzip')
post "/v1/devices/#{device.serial_number}/scans.json", params: { data: scan }
expect(response).to be_successful
end
end
我的控制是比較基本的,以及:
class V1::ScansController < ApplicationController
skip_before_action :authenticate_user_from_token!, only: [:create]
skip_before_action :authenticate_user!, only: [:create]
skip_before_filter :verify_authenticity_token, :if => Proc.new { |c| c.request.format == 'application/json' }, only: [:create]
def create
@scan = Scan.new(scan_params)
respond_to do |format|
if @scan.save
# GenerateReport.perform_async(@scan.id)
format.json { render json: @scan, status: :created }
else
format.json { render json: @scan.errors, status: :unprocessable_entity }
end
end
end
private
def set_device
@device = Device.find_by!(serial_number: params[:device_serial_number])
end
def scan_params
params.require(:scan).permit(:device_serial_number, :data)
end
end
和我的路線文件有:
api_version(:module => "V1", :path => {:value => "v1"}) do
resources :devices, only: [:show, :update], param: :serial_number do
resources :scans, only: [:create]
end
end
其產生以下路線:
v1_device_scans POST /v1/devices/:device_serial_number/scans(.:format) v1/scans#create
因此,很明顯我的路線是有...我缺少什麼?
問題來自'post「/ v1/devices /#{device.serial_number} /scans.json」,'你需要給它一個映射的路由符號。 –
你是什麼意思?類似於'post:create,params:{device_serial_number:device.serial_number}' – Godzilla74
@ MathewP.Jones它看起來像構建url一樣,我做了'device.serial_number'實際上並沒有被放入字符串。 – Godzilla74