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

时间:2012-02-23 21:44:13

标签: ruby-on-rails-3 testing rspec gem

forked the country_code_select gem并制作了fix,因为它没有为嵌套的fields_for模型正确生成ID属性。

我以前从未写过测试或打过宝石 - 但我正在尝试。这对我来说是全新的。

你能帮我写一下fix的测试用例,以便我可以提交我的第一个宝石补丁吗?

我尝试在form_for中使用fields_forspec/form_helpers_spec,但这只是让我陷入了一个兔子洞。


可能有用的其他信息:

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) %>
...

1 个答案:

答案 0 :(得分:0)

你已经有了测试!在你的分叉回购中,有this line。只需复制此测试用例以创建新的比较,如果您的修改可以正常工作,并且不会制动旧规格

如果您需要更多示例或学校教育,请查看how rails tests view helpers。但请注意,他们为自己编写这些帮助程序,并使用其他测试框架而不是fork。

不太准确的例子:

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帮助我生成更好,更可靠的代码,我认为值得额外的努力。

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

相关问题