如何使用RSPEC在没有铁轨的红宝石宝石中测试控制器的问题?
我想我可能会以错误的方式解决这个问题。我正在创建一个红宝石宝石来容纳一些共享功能。此功能的一部分是一个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论