在其他测试中重用 RSpec 测试

发布于 2024-12-18 07:49:33 字数 588 浏览 0 评论 0原文

我对 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 技术交流群。

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

发布评论

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

评论(2

蝶舞 2024-12-25 07:49:33

使用共享示例。在您的情况下,类似这样的方法可能会起作用:

# spec/shared_examples_for_form.rb
shared_examples 'a form' do
  describe 'validation' do
    it 'should be validated' do
      form.should_be valid
    end
  end
end

# spec/requests/user_form.rb
describe UserForm
  it_behaves_like 'a form' do
    let(:form) { find(:tag, 'form') }
  end
end

也可以将参数传递给共享示例并将共享示例放置在 spec/support 中。只需阅读文档

Use shared examples. In you case, something like this may work:

# spec/shared_examples_for_form.rb
shared_examples 'a form' do
  describe 'validation' do
    it 'should be validated' do
      form.should_be valid
    end
  end
end

# spec/requests/user_form.rb
describe UserForm
  it_behaves_like 'a form' do
    let(:form) { find(:tag, 'form') }
  end
end

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.

时间海 2024-12-25 07:49:33

共享的示例很棒,但是这里至少有两个严重的问题。

第一:为什么要提供表单字段 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.

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