[英]Cannot stub Rails.env inside Grape::API class
i'm trying to stub anything that inside this class:我试图存根这个类中的任何东西:
module V1
class API < Grape::API
use V1::Middleware::ApiLogger if Rails.env.production?
and after in the spec:在规范之后:
allow(Rails).to receive(:env).and_return(double(production?: true))
or或者
allow(Rails.env).to receive(:production?).and_return(true)
or或者
allow(Rails).to receive(:env) { "production".inquiry }
and和
expect do
get "/api/v1/profiles/#{profile_alex.id}"
end.to change(ApiLog, :count).by 1
Nothing is working, even stubbing instance method inside API class won't work什么都不起作用,即使在 API 类中存根实例方法也不起作用
Did anybody has faced same problem?有没有人遇到过同样的问题?
I've found the problem.我找到了问题所在。
This was related to how the Grape preloads API class, it uses mount method that runs when the code is loaded and stub was running after the conditions, so the checks for environment shall go inside Middleware methods.这与 Grape 如何预加载 API 类有关,它使用加载代码时运行的 mount 方法,并且在条件之后运行存根,因此环境检查应在中间件方法中进行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.