Rswag 似乎是在集成测试中指定 API 的一个很酷的工具。这是我第一次尝试,但停留在 FactoryBot 创建中。以下是我所做的:
在 rails_helper.rb 中,添加了 require 和 load 支持帮助文件:
require 'rspec/rails'
require 'factory_bot_rails'
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
在 spec_helper.rb 中,添加了支持助手:
RSpec.configure do |config|
config.color = true
config.order = :random
config.include RequestHelpers, type: :request
在 spec/support 文件夹中,一个文件用于 FactoryBot,一个文件用于一些助手:
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
end
module RequestHelpers
include Rack::Test::Methods
def json
JSON.parse(last_response.body)
end
def get_with_token(path, params={})
header "Authorization", "Bearer #{gen_jwt}"
get path, params
end
def gen_jwt
payload = {email: 'user@test.com'}
token = JWT.encode(payload, Rails.application.config.jwt_secret, 'HS256')
end
end
创建为 spec/factories/node.rb 的工厂文件
FactoryBot.define do
factory :node, class: Node do
fqdn { "node_single.test.com" }
Platform { "ubuntu" }
IP { "192.168.99.1" }
isDeleted { false }
end
factory :node_list .... # create a list with sequence
然后,通过了以下传统 rspec api 测试:
# spec/controllers/api/v1/nodes_controller_spec.rb
RSpec.describe 'nodes', type: :request do
describe 'index' do
context 'pagination' do
before(:each) do
create_list(:node_list, 32)
end
it 'get first page' do
ops = { pageSize: 10, pageNumber: 1 }
get_with_token "/api/v1/nodes", ops
expect(json["meta"]["total-record-number"]).to eq(32)
expect(json["data"].length).to eq(10)
end
end
end
但是当试图开发一个 rswag 测试文件时,它失败了......
require 'swagger_helper'
describe 'Nodes API', type: :request, swagger_doc: 'api/v1/swagger.json' do
TAGS_NODE = Node.freeze
path '/api/v1/nodes' do
get 'Get all servers' do
tags TAGS_NODE
produces 'application/json'
parameter name: :searchString, in: :query, type: :string
parameter name: :searchColumn, in: :query, type: :string
parameter name: :Authorization, in: :header, type: :string
let(:Authorization) { 'Bearer ' + gen_jwt }
let!(:nodes) { create_list(:node_list, 32) }
response '200', 'Servers found' do
let(:searchString) { '' }
let(:searchColumn) { '' }
run_test! do |repsonse|
data = JSON.parse(response.body)
puts data
end
end
end
end
end
它失败并出现以下错误:
rspec spec/integration/api/v1/nodes_spec.rb
Randomized with seed 18293
F
Failures:
1) Nodes API /api/v1/nodes get Servers found returns a 200 response
Failure/Error: let!(:nodes) { create_list(:node_list, 32) }
SystemStackError:
stack level too deep
# /usr/local/rvm/gems/ruby-2.4.5/gems/activesupport-5.2.3/lib/active_support/inflector/methods.rb:405:in `block in apply_inflections'
# /usr/local/rvm/gems/ruby-2.4.5/gems/activesupport-5.2.3/lib/active_support/inflector/methods.rb:405:in `apply_inflections'
# /usr/local/rvm/gems/ruby-2.4.5/gems/activerecord-5.2.3/lib/active_record/core.rb:242:in `inspect'
# /usr/local/rvm/gems/ruby-2.4.5/gems/activerecord-5.2.3/lib/active_record/core.rb:242:in `inspect'
......
# /usr/local/rvm/gems/ruby-2.4.5/gems/factory_bot-5.1.0/lib/factory_bot/decorator/new_constructor.rb:9:in `new'
# /usr/local/rvm/gems/ruby-2.4.5/gems/factory_bot-5.1.0/lib/factory_bot/decorator.rb:18:in `send'
# /usr/local/rvm/gems/ruby-2.4.5/gems/factory_bot-5.1.0/lib/factory_bot/decorator.rb:10:in `method_missing'
# /usr/local/rvm/gems/ruby-2.4.5/gems/factory_bot-5.1.0/lib/factory_bot/decorator/invocation_tracker.rb:11:in `method_missing'
# /usr/local/rvm/gems/ruby-2.4.5/gems/factory_bot-5.1.0/lib/factory_bot/decorator.rb:18:in `send'
# /usr/local/rvm/gems/ruby-2.4.5/gems/factory_bot-5.1.0/lib/factory_bot/decorator.rb:10:in `method_missing'
# /usr/local/rvm/gems/ruby-2.4.5/gems/factory_bot-5.1.0/lib/factory_bot/configuration.rb:23:in `block in initialize'
# /usr/local/rvm/gems/ruby-2.4.5/gems/factory_bot-5.1.0/lib/factory_bot/attribute_assigner.rb:51:in `instance_exec'
# /usr/local/rvm/gems/ruby-2.4.5/gems/factory_bot-5.1.0/lib/factory_bot/attribute_assigner.rb:51:in `build_class_instance'
# /usr/local/rvm/gems/ruby-2.4.5/gems/factory_bot-5.1.0/lib/factory_bot/attribute_assigner.rb:13:in `object'
# /usr/local/rvm/gems/ruby-2.4.5/gems/factory_bot-5.1.0/lib/factory_bot/evaluation.rb:13:in `object'
# /usr/local/rvm/gems/ruby-2.4.5/gems/factory_bot-5.1.0/lib/factory_bot/strategy/create.rb:9:in `result'
# /usr/local/rvm/gems/ruby-2.4.5/gems/factory_bot-5.1.0/lib/factory_bot/factory.rb:43:in `run'
# /usr/local/rvm/gems/ruby-2.4.5/gems/factory_bot-5.1.0/lib/factory_bot/factory_runner.rb:29:in `block in run'
# /usr/local/rvm/gems/ruby-2.4.5/gems/factory_bot-5.1.0/lib/factory_bot/factory_runner.rb:28:in `run'
# /usr/local/rvm/gems/ruby-2.4.5/gems/factory_bot-5.1.0/lib/factory_bot/strategy_syntax_method_registrar.rb:20:in `block in define_singular_strategy_method'
# /usr/local/rvm/gems/ruby-2.4.5/gems/factory_bot-5.1.0/lib/factory_bot/strategy_syntax_method_registrar.rb:32:in `block (2 levels) in define_list_strategy_method'
# /usr/local/rvm/gems/ruby-2.4.5/gems/factory_bot-5.1.0/lib/factory_bot/strategy_syntax_method_registrar.rb:32:in `initialize'
# /usr/local/rvm/gems/ruby-2.4.5/gems/factory_bot-5.1.0/lib/factory_bot/strategy_syntax_method_registrar.rb:32:in `new'
# /usr/local/rvm/gems/ruby-2.4.5/gems/factory_bot-5.1.0/lib/factory_bot/strategy_syntax_method_registrar.rb:32:in `block in define_list_strategy_method'
# ./spec/integration/api/v1/nodes_spec.rb:14:in `block (4 levels) in <top (required)>'
痕迹很长,我想我想把某事有用
在stackoverflow中也做了一些谷歌搜索,但找不到任何线索。非常感谢你的帮助!
更新
更改为使用 'before' 而不是 let 时出现同样的错误,在 rswag 集成案例中无法在数据库中创建记录,但它在模型规范和控制器规范中确实有效......