如何从控制台访问 Rails 测试帮助程序代码?

发布于 2025-01-02 18:35:21 字数 664 浏览 1 评论 0原文

使用普通的 ActiveSupport::TestCase,我添加了几个在 test/test_helper.rb 中生成测试模型对象的帮助器方法。此文件中重新打开了包含帮助器方法的 ActiveSupport::TestCase。

现在假设我在 Ruby 控制台(或 pry,加载了 Rails 环境),并且我想摆弄这些测试方法。我可以加载文件:加载“test/test_helper.rb”,但我无法创建一个 TestCase 实例来让我访问这些测试方法来使用它们:

[4] pry(main)> tc = ActiveSupport::TestCase.new
ArgumentError: wrong number of arguments (0 for 1)
[5] pry(main)> tc = ActiveSupport::TestCase.new(1)
TypeError: 1 is not a symbol
[6] pry(main)> tc = ActiveSupport::TestCase.new(:unit)
ArgumentError: uncaught throw :invalid_test

我徒劳地尝试跟踪源代码中的面包屑痕迹文件来查找 TestCase 初始化方法,这样我就可以弄清楚它在这里坚持的内容。

有人可以帮助我如何思考这样的问题并找出解决方法吗?

Using vanilla ActiveSupport::TestCase, I've added several helper methods that generate test model objects in test/test_helper.rb. In this file is a reopening of ActiveSupport::TestCase that contains the helper methods.

Now suppose I'm in the Ruby Console (or pry, with the Rails environment loaded), and I want to fiddle with these test methods. I can load the file: load "test/test_helper.rb" but I can't create a TestCase instance that will let me access those test methods to play with them:

[4] pry(main)> tc = ActiveSupport::TestCase.new
ArgumentError: wrong number of arguments (0 for 1)
[5] pry(main)> tc = ActiveSupport::TestCase.new(1)
TypeError: 1 is not a symbol
[6] pry(main)> tc = ActiveSupport::TestCase.new(:unit)
ArgumentError: uncaught throw :invalid_test

I've tried in vain to follow the trail of breadcrumbs in source files to find the TestCase initialize method so I could figure out what it's insisting on here.

Can someone help me with how to think about problems like this and figure out a workaround?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紧拥背影 2025-01-09 18:35:21

从此链接复制: https://gist.github.com/1297510

在您的终端上键入:

RAILS_ENV=test pry

Copied from this link: https://gist.github.com/1297510

On your terminal type:

RAILS_ENV=test pry
初心未许 2025-01-09 18:35:21

对于您的具体情况,在我的 ActiveSupport (3.22.2.8) 版本中,我能够执行以下操作:

ActiveSupport::TestCase.new('some_string')

不过,一般来说,有几个选项:

  • 使用像 pry 这样的调试器可能会有所帮助,以单步执行通过这个问题。
  • 通过 gem open activesupport 查找源代码,或者通过 gem 环境查找您的 gem 的安装位置。
  • 大多数 gem 都是开源的,可以在 github.com 等上轻松找到。请注意您正在查看正确的版本。

For your specific case, in my version of ActiveSupport (3.22.2.8), I was able to do the following:

ActiveSupport::TestCase.new('some_string')

In general though, there's several options:

  • using a debugger like pry can be helpful, to step through the issue.
  • find the source code via gem open activesupport or find where your gems are installed with gem environment
  • most gems are open source and are easily found on github.com, etc. Just be careful that you're looking at the correct version.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文