如何使用RSPEC在没有铁轨的红宝石宝石中测试控制器的问题?

发布于 2025-01-20 19:17:04 字数 814 浏览 0 评论 0原文

我想我可能会以错误的方式解决这个问题。我正在创建一个红宝石宝石来容纳一些共享功能。此功能的一部分是一个ActiveSupport ::关注点,其中包括fore_action回调。在宝石中看起来像这样的东西:

require "active_support"

module MyModule
  module MyConcern
    extend ::ActiveSupport::Concern

    included do
      before_action :foo
    end

  private

    def foo
      # Some implementation
    end
  end

换句话说,我将其包括在我的宝石中。

到目前为止,一切都很好,但现在我正在尝试使用普通RSPEC对此进行测试,而我迷路了。因为我的宝石不使用导轨(只是为了担心的是ActiveSupport),所以我不确定如何在没有RSPEC轨道的情况下测试此功能。通常在Rails应用程序中,我会测试控制器的行为,好像foo在任何操作之前都调用,但是如果没有RSPEC-rails,我无法访问任何方法来测试控制器。

此外,我会喜欢一种创建shared_example的方法需要具有RSPEC控制器语法,例如get:index随附RSPEC-rails。

我是否以错误的方式考虑这一点?我应该将铁轨加入我的小宝石中,以便我可以正确测试吗?我要以错误的方式测试这个吗?在宝石中加入这样的问题是错误的吗?

I think I may be going about this the wrong way. I'm creating a Ruby gem to house some shared functionality. One piece of this functionality is an ActiveSupport::Concern that includes a before_action callback. That's looking something like this inside the gem:

require "active_support"

module MyModule
  module MyConcern
    extend ::ActiveSupport::Concern

    included do
      before_action :foo
    end

  private

    def foo
      # Some implementation
    end
  end

In other words I'm including this in my gem so any codebase using my gem can include the concern on a controller and automatically have the foo method run before every action.

That all works fine and dandy so far, but now I'm at the point where I'm trying to test this using plain Rspec and I'm lost. Because my gem doesn't use rails (just ActiveSupport for the concern) I'm not sure how to go about testing this functionality without Rspec-Rails. Usually in a rails app, I'd test the behavior of the controller is working as if foo was called before any action, but without Rspec-Rails I don't have access to any way to test controllers.

In addition I'd love a way to create a shared_example for this concern that can be used in any app that uses the gem to verify the concern is working correctly on each controller, but again I think that would require having Rspec controller syntax like get :index that comes along with Rspec-Rails.

Am I thinking about this the wrong way? Should I be including Rails in my tiny little gem just so I can test this properly? Am I going about testing this the wrong way? Is it wrong to be including a concern like this in a gem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文