为自定义 FormHelper gem 补丁编写测试
我分叉了country_code_select gem并制作了一个修复,因为它没有为嵌套 fields_for 模型正确生成 ID 属性。
我以前从未编写过测试或修补过 gem - 但我正在尝试。这对我来说是全新的。
您能否帮我为我的修复编写测试用例,以便我可以提交我的第一个修复宝石补丁?
我尝试在 spec/form_helpers_spec
中使用 form_for
和 fields_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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你已经有测试了!在您分叉的存储库中,有这一行。如果您的修改按预期工作并且不破坏旧规范,只需复制此测试用例即可创建新的比较。
如果您需要更多示例或培训,请查看如何Rails 测试视图助手。但请注意,他们为自己编写这些帮助程序,并且他们使用除您的分支之外的其他测试框架。
不太准确的例子:
如果您想更深入地了解这一点,我还建议您阅读有关测试的内容。有很多内容,但我发现 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:
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 :)