Hello.
Test::Nginx 可以实现你的需求,只要你的想象力足够大:), 依赖于 lua vm 的 package.loaded 全局变量,
在 master init 阶段 require 一次模块,然后替换这个模块为你的 mock
的模块即可。下面是从运行在生产的代码的范例(无法运行,删除了隐私部分)。
our $http_config = <<"_EOC_";
lua_package_path '$workdir/?.lua;;';
lua_package_cpath '$workdir/xx/clib/?.so;;';
init_by_lua_block {
-- we mock the socket
require("xx.lib.resty.logger.socket")
package.loaded["xx.lib.resty.logger.socket"] =
require("xx.mock.resty-logger-socket")
local pid = ngx.worker.pid
ngx.worker.pid = function()
return 1993
end
}
_EOC_
repeat_each(1);
no_shuffle();
run_tests();
__DATA__
=== TEST1: test 1
--- http_config eval: $::http_config
--- config
location = /app/active {
content_by_lua_block {
xxxx
}
}
......