在其他测试中重用 RSpec 测试
我对 HTML 标记非常严格,并且遵循严格的表单、列表等编码约定...
我想在我的 RSpec 测试中包含可重用测试,这将允许我从任何地方调用表单测试其他测试并将其直接定位到我正在测试的页面或 URL。
像这样的事情:
# spec/helpers/form_tester.rb
describe FormTester
it "should check to see if all the text fields have an ID prefix of 'input-'" do
... @form should be valid ...
should be true
end
end
# spec/requests/user_form.rb
describe UserForm
it "should validate the form" do
@form = find(:tag,'form')
# call the FormTester method
end
end
关于如何做到这一点有什么想法吗?我正在使用 Rails 3.1、RSpec、Capybara 和 FactoryGirl。
I'm very rigorous when it comes to my HTML markup and I follow a strict coding convention for forms, lists, etc...
I would like to include reusable test in my RSpec tests that would allow for me call a form test from any other test and target it directly to the page or URL that I'm testing.
Something like this:
# spec/helpers/form_tester.rb
describe FormTester
it "should check to see if all the text fields have an ID prefix of 'input-'" do
... @form should be valid ...
should be true
end
end
# spec/requests/user_form.rb
describe UserForm
it "should validate the form" do
@form = find(:tag,'form')
# call the FormTester method
end
end
Any ideas on how todo this? I'm using Rails 3.1, with RSpec, Capybara and FactoryGirl.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用共享示例。在您的情况下,类似这样的方法可能会起作用:
也可以将参数传递给共享示例并将共享示例放置在
spec/support
中。只需阅读文档 。Use shared examples. In you case, something like this may work:
It's also possible to pass parameters to shared examples and place the shared examples inside
spec/support
. Just have a read at the documentation.共享的示例很棒,但是这里至少有两个严重的问题。
第一:为什么要提供表单字段 ID?您已经拥有了非常好的选择器:只需使用
input[name='whatever']
。即使您给了他们 ID,也不要在它们上面添加前缀:input#whatever
或只是#whatever
可能更合适CSS 中比#input-whatever
更明智的选择器。如果选择器名称过于具体,很可能会使 CSS 和 JavaScript 更难编写。第二:不要使用 RSpec 来测试你的观点。当仅限于模型时,RSpec 处于最佳状态。 Cucumber 更适合任何面向用户的东西。
Shared examples are great, but you've got at least two serious problems here.
First: why are you giving your form fields IDs at all? You've already got perfectly good selectors: just use
input[name='whatever']
. And even if you are giving them IDs, don't put a prefix on them:input#whatever
or just#whatever
is probably a more sensible selector in your CSS than#input-whatever
. By being overspecific on your selector names, you're most likely making your CSS and JavaScript harder to write than they have to be.Second: don't use RSpec to test your views. RSpec is at its best when confined to models. Cucumber is better for anything user-facing.