为自定义 FormHelper gem 补丁编写测试

发布于 2025-01-08 09:30:21 字数 1007 浏览 2 评论 0原文

分叉了country_code_select gem并制作了一个修复,因为它没有为嵌套 fields_for 模型正确生成 ID 属性。

我以前从未编写过测试或修补过 gem - 但我正在尝试。这对我来说是全新的。

您能否帮我为我的修复编写测试用例,以便我可以提交我的第一个修复宝石补丁?

我尝试在 spec/form_helpers_spec 中使用 form_forfields_for 但这只是让我陷入了困境。


可能有帮助的其他信息:

Client.rb

has_one :billing_address
accepts_nested_attributes_for :billing_address

查看:

<%= form_for @client do |f| %>
  <%= f.fields_for :billing_address, @client.billing_address  do |ff| %>
    <%= ff.country_code_select(:country_code) %>
...

I forked the country_code_select gem and made a fix because it wasn't generating the ID attribute properly for nested fields_for models.

I've never written tests before or patched a gem - but I'm trying. This is entirely new to me.

Can you help me write the test case for my fix so that I can submit my first ever gem patch?

I tried using form_for and fields_for in spec/form_helpers_spec but that just took me down a rabbit hole.


Additional info that may help:

Client.rb

has_one :billing_address
accepts_nested_attributes_for :billing_address

View:

<%= form_for @client do |f| %>
  <%= f.fields_for :billing_address, @client.billing_address  do |ff| %>
    <%= ff.country_code_select(:country_code) %>
...

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

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

发布评论

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

评论(1

瞳孔里扚悲伤 2025-01-15 09:30:21

你已经有测试了!在您分叉的存储库中,有这一行。如果您的修改按预期工作并且不破坏旧规范,只需复制此测试用例即可创建新的比较。

如果您需要更多示例或培训,请查看如何Rails 测试视图助手。但请注意,他们为自己编写这些帮助程序,并且他们使用除您的分支之外的其他测试框架。

不太准确的例子:

it "should output a valid select field for fields_for nested attributes" do
  # in the next line, pass such parameters to this function
  # so that your particular modification to the code is triggered
  output = country_code_select(:client, :billing_address, :country_code)

  # and here check if your code works as it should
  # I think you're modifying how id is assigned with nesting fields,
  # so test something like this:
  output.should match(/select id="client_billing_address_country_code"/)
end

如果您想更深入地了解这一点,我还建议您阅读有关测试的内容。有很多内容,但我发现 TDD 有帮助我希望能够编写出更好、更可靠的代码,我认为付出额外的努力是值得的。

由于这是您的第一次贡献,欢迎来到开源社区,祝您的贡献好运:)

You have tests already! In you forked repo, there's this line. Just copy this test case to create a new comparison if your modification works as it should and without braking the old specs.

If you need more examples or schooling, look at how rails tests view helpers. Be aware though, that they write these helpers for themselves and they use other testing framework than your fork.

Not so accurate example:

it "should output a valid select field for fields_for nested attributes" do
  # in the next line, pass such parameters to this function
  # so that your particular modification to the code is triggered
  output = country_code_select(:client, :billing_address, :country_code)

  # and here check if your code works as it should
  # I think you're modifying how id is assigned with nesting fields,
  # so test something like this:
  output.should match(/select id="client_billing_address_country_code"/)
end

I also recommend reading about tests, if you want to get deeper with this. There's a lot to it, but I found that TDD helped me to produce better, more reliable code and I think it's worth the extra effort.

Since it's your first contribution, Welcome to the Open Source community and good luck with your contributions :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文