有问题的Rails应用程序在测试中使用Rspec,Capybara和WebMock。
在一个测试方案中,此代码有效:
response = RestClient.post 'the.domain.com',
{
"dados": {
"head": {
"servico": "autenticacao",
"chave": ""
},
"data": {
"empresa": "my_name",
"login": "my_login",
"senha": "my_password"
}
}
}.to_json
RSpec.configure do |config|
config.before(:each) do
stub_request(:any, 'the.domain.com').
with( body: hash_including(
{"dados": {
"head": {
"servico": "autenticacao",
"chave": ""
},
"data": {
"empresa": "my_name",
"login": "my_login",
"senha": "my_paasword"
}
}}.to_json
) ).to_return(
status: 200,
body: {"dados": {"btms": {"chave": "returned_key"}}}.to_json,
headers: {}
)
end
end
但是,在同一情况下,这种情况不会发生:
data = []
data << {
"cdg_tipo_transporte": "3"
}
response = RestClient.post 'the.domain.com',
{
"dados": {
"head": {
"servico": "reserva_salva_lote",
"chave": response["dados"]["btms"]["chave"]
},
"data": data
}
}.to_json
RSpec.configure do |config|
config.before(:each) do
data = []
data << {
"cdg_tipo_transporte": "3"
}
stub_request(:any, 'the.domain.com').
with( body: hash_including(
{"dados": {
"head": {
"servico": "reserva_salva_lote",
"chave": "returned_key"
},
"data": data
} }.to_json
) ).to_return(
status: 200,
body: {"Status?": "OK"}.to_json,
headers: {}
)
end
end
我得到的错误是关于如何禁用真实的HTTP连接,这是未注册的请求。我认为这表示Rails和/或WebMock与json不匹配。
我研究了不同的事物并尝试了不同的事物。似乎方括号是问题所在。当我没有它们构建几乎所有东西时,它都可以工作。当我放回它们时,代码再次中断。
在这种情况下,如何使用方括号?我需要它们在那儿,因为我要发布的真实API需要这种格式。
顺便说一句,开发环境与实际的API通信,并且代码工作正常。人体测试是完美的。只是自动测试不断失败!
更新
这是错误堆栈:
保存响应的变量说<WebMock::NetConnectNotAllowedError: Real HTTP connections are disabled. Unregistered request:
然后显示请求,建议的存根(与我的存根匹配)和已注册的存根。
在此之下,我得到:
#<ActionDispatch::Cookies::CookieOverflow: ActionDispatch::Cookies::CookieOverflow>
/Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/actionpack-5.1.6/lib/action_dispatch/middleware/cookies.rb:594:in `commit'
/Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/actionpack-5.1.6/lib/action_dispatch/middleware/cookies.rb:465:in `[]='
/Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/actionpack-5.1.6/lib/action_dispatch/middleware/session/cookie_store.rb:117:in `set_cookie'
/Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/rack-2.0.5/lib/rack/session/abstract/id.rb:363:in `commit_session'
/Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/rack-2.0.5/lib/rack/session/abstract/id.rb:234:in `context'
/Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/rack-2.0.5/lib/rack/session/abstract/id.rb:226:in `call'
/Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/actionpack-5.1.6/lib/action_dispatch/middleware/cookies.rb:613:in `call'
/Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/actionpack-5.1.6/lib/action_dispatch/middleware/callbacks.rb:26:in `block in call'
/Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/activesupport-5.1.6/lib/active_support/callbacks.rb:97:in `run_callbacks'
/Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/actionpack-5.1.6/lib/action_dispatch/middleware/callbacks.rb:24:in `call'
/Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/actionpack-5.1.6/lib/action_dispatch/middleware/debug_exceptions.rb:59:in `call'
/Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/actionpack-5.1.6/lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
/Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/railties-5.1.6/lib/rails/rack/logger.rb:36:in `call_app'
/Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/railties-5.1.6/lib/rails/rack/logger.rb:24:in `block in call'
/Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/activesupport-5.1.6/lib/active_support/tagged_logging.rb:69:in `block in tagged'
/Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/activesupport-5.1.6/lib/active_support/tagged_logging.rb:26:in `tagged'
/Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/activesupport-5.1.6/lib/active_support/tagged_logging.rb:69:in `tagged'
/Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/railties-5.1.6/lib/rails/rack/logger.rb:24:in `call'
/Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/actionpack-5.1.6/lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
/Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/actionpack-5.1.6/lib/action_dispatch/middleware/request_id.rb:25:in `call'
/Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/rack-2.0.5/lib/rack/method_override.rb:22:in `call'
/Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/rack-2.0.5/lib/rack/runtime.rb:22:in `call'
/Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/activesupport-5.1.6/lib/active_support/cache/strategy/local_cache_middleware.rb:27:in `call'
/Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/actionpack-5.1.6/lib/action_dispatch/middleware/executor.rb:12:in `call'
/Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/actionpack-5.1.6/lib/action_dispatch/middleware/static.rb:125:in `call'
/Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/rack-2.0.5/lib/rack/sendfile.rb:111:in `call'
/Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/railties-5.1.6/lib/rails/engine.rb:522:in `call'
/Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/rack-2.0.5/lib/rack/urlmap.rb:68:in `block in call'
/Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/rack-2.0.5/lib/rack/urlmap.rb:53:in `each'
/Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/rack-2.0.5/lib/rack/urlmap.rb:53:in `call'
/Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/capybara-3.2.1/lib/capybara/server/middleware.rb:44:in `call'
/Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/puma-3.11.4/lib/puma/configuration.rb:225:in `call'
/Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/puma-3.11.4/lib/puma/server.rb:632:in `handle_request'
/Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/puma-3.11.4/lib/puma/server.rb:446:in `process_client'
/Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/puma-3.11.4/lib/puma/server.rb:306:in `block in run'
/Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/puma-3.11.4/lib/puma/thread_pool.rb:120:in `block in spawn_thread'
AND
Failure/Error: raise CookieOverflow if options[:value].bytesize > MAX_COOKIE_SIZE
ActionDispatch::Cookies::CookieOverflow:
ActionDispatch::Cookies::CookieOverflow
# /Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/rack-2.0.5/lib/rack/session/abstract/id.rb:363:in `commit_session'
# /Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/rack-2.0.5/lib/rack/session/abstract/id.rb:234:in `context'
# /Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/rack-2.0.5/lib/rack/session/abstract/id.rb:226:in `call'
# /Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/railties-5.1.6/lib/rails/rack/logger.rb:36:in `call_app'
# /Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/railties-5.1.6/lib/rails/rack/logger.rb:24:in `block in call'
# /Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/railties-5.1.6/lib/rails/rack/logger.rb:24:in `call'
# /Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/rack-2.0.5/lib/rack/method_override.rb:22:in `call'
# /Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/rack-2.0.5/lib/rack/runtime.rb:22:in `call'
# /Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/rack-2.0.5/lib/rack/sendfile.rb:111:in `call'
# /Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/railties-5.1.6/lib/rails/engine.rb:522:in `call'
# /Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/rack-2.0.5/lib/rack/urlmap.rb:68:in `block in call'
# /Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/rack-2.0.5/lib/rack/urlmap.rb:53:in `each'
# /Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/rack-2.0.5/lib/rack/urlmap.rb:53:in `call'
# /Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/capybara-3.2.1/lib/capybara/server/middleware.rb:44:in `call'
# /Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/puma-3.11.4/lib/puma/configuration.rb:225:in `call'
# /Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/puma-3.11.4/lib/puma/server.rb:632:in `handle_request'
# /Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/puma-3.11.4/lib/puma/server.rb:446:in `process_client'
# /Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/puma-3.11.4/lib/puma/server.rb:306:in `block in run'
# /Users/ViviPoit/.rvm/gems/ruby-2.3.7/gems/puma-3.11.4/lib/puma/thread_pool.rb:120:in `block in spawn_thread'
答案 0 :(得分:1)
hash_including
使用键名和键值对来匹配,您正在向其传递{...}.to_json
,因此它将尝试将整个json值匹配为键。
另外,您的第二个请求存根期望dados.head.chave
等于"1234abcd"
,但是第一个存根在"returned_key"
中返回response["dados"]["btms"]["chave"]
,显然存根不匹配,导致{{1} }。