为 API 规范在 rspec 中添加 protect_from_forgery

Stubbing protect_from_forgery in rspec for API specs

我正在为我的 Rails 应用构建一个 api,并希望使用 protect_from_forgery 保护它免受 CSRF 攻击。我正在为 ApiController 编写请求规范,并希望为请求中的 CSRF 无效时得到的响应编写规范。我的问题是如何对 protect_from_forgery 的行为进行存根以模仿无效的 CSRF 令牌?

allow(ApiController).to receive(:protect_from_forgery).and_return(false)