我试图了解Rspec和Controller测试,更具体地说是JSON请求。在这种情况下,我试图点击v1_devices_path
路线:
的routes.rb
v1_device_scans POST /v1/devices/:device_serial_number/scans(.:format) v1/scans#create
v1_device GET /v1/devices/:serial_number(.:format) v1/devices#show
PATCH /v1/devices/:serial_number(.:format) v1/devices#update
PUT /v1/devices/:serial_number(.:format) v1/devices#update
我的控制员:
控制器/ V1 / devices_controller.rb
class V1::DevicesController < ApplicationController
before_action :set_device, only: [:show, :update]
respond_to :json
def show
@device = Device.find_by(serial_number: params[:serial_number])
render :json => @device.as_json
end
def update
if @device.update(device_params)
render json: @device, status: :ok, location: @device
else
render json: @device.errors, status: :unprocessable_entity
end
end
private
def set_device
@device = Device.find_by!(serial_number: params[:serial_number])
end
def device_params
params.require(:device).permit(
:serial_number,
:name,
:diagnostic_checkin_status,
:diagnostic_dns,
:diagnostic_ping,
:assigned_internal_ip,
:assigned_external_ip,
:assigned_gateway_ip,
:version,
:timezone,
:task_id,
:scan_status,
:scan_progress
)
end
end
截至目前,我的测试非常简单......只是想确保我得到一些结果:
规格/控制器/ V1 / devices_controller_spec.rb
要求&#39; rails_helper&#39;
RSpec.describe V1::DevicesController, type: :controller do
it "shows device info" do
device = FactoryGirl.create(:device)
get v1_device_path(device.serial_number), :format => :json
expect(response.status).to be(200)
end
end
经过一些调整后,我已经达到了正确创建我的网址的程度,但我仍然遇到no route matches
错误:
1) V1::DevicesController shows device info
Failure/Error: get v1_device_path(device.serial_number), :format => :json
ActionController::UrlGenerationError:
No route matches {:action=>"/v1/devices/41442305171c430ab253ba1ad95c5c61", :controller=>"v1/devices", :format=>:json}
# /usr/local/bundle/gems/rails-controller-testing-1.0.2/lib/rails/controller/testing/template_assertions.rb:61:in `process'
# /usr/local/bundle/gems/devise-4.3.0/lib/devise/test/controller_helpers.rb:33:in `block in process'
# /usr/local/bundle/gems/devise-4.3.0/lib/devise/test/controller_helpers.rb:100:in `catch'
# /usr/local/bundle/gems/devise-4.3.0/lib/devise/test/controller_helpers.rb:100:in `_catch_warden'
# /usr/local/bundle/gems/devise-4.3.0/lib/devise/test/controller_helpers.rb:33:in `process'
# /usr/local/bundle/gems/rails-controller-testing-1.0.2/lib/rails/controller/testing/integration.rb:12:in `block (2 levels) in <module:Integration>'
# ./spec/controllers/v1/devices_controller_spec.rb:11:in `block (2 levels) in <top (required)>'
我做错了什么?
答案 0 :(得分:-1)
好的,从@sebastián的评论开始,我将规格更改为:
*** Failure 3552 Cannot DROP databases with tables, journal tables,
views, macros, or zones.
Statement# 1, Info =0
*** Total elapsed time was 1 second.
现在我的考试通过了。
答案 1 :(得分:-1)
这是另一种不将type
更改为request
的解决方案。出现此错误是因为在serial_number
设计路线中添加了show
参数,您可以在serial_number
网址中添加get
。
RSpec.describe V1::DevicesController, type: :controller do
it "shows device info" do
device = FactoryGirl.create(:device)
get :show, params: { serial_number: device.serial_number }, :format => :json
expect(response.status).to be(200)
end
end
我希望这对你有所帮助。