用于没有文字描述的测试的 Rspec 标签?

发布于 2024-12-19 03:45:10 字数 504 浏览 0 评论 0原文

非常简单的问题,但我在文档中找不到答案。 如何向缺少文本描述的特定 Rspec 示例添加标签?例如,如果您使用相当常见的“:focus”标签来选择要运行的测试(尽管这不是我需要标签的唯一用途),那么通常您会写:

specify "the value should be 2", :focus => true do 
  @value.should be(2) 
end

但是,这并不罕见对于简单的测试以避免冗余,让测试本身进行对话并省略“值应该是 2”元素,给您类似的内容:

context "the value" do
  subject{@value}
  it {should be(2)}
  it {should_not be(3)
end

但是,我不知道如何向此类测试添加标签。这可能吗?像这样的东西:

it :focus {should have_content("Oh yeah")}

Pretty simple question, but I can't find an answer in the docs.
How can I add tags to specific Rspec examples that lack textual descriptions? For example, if you are using the fairly common ":focus" tag to select the tests you want to run (though that's not the only thing I need the tags for), you would, normally, write:

specify "the value should be 2", :focus => true do 
  @value.should be(2) 
end

However, it is not uncommon for simple tests to avoid redundancy to let the test itself do the talking and leave out the "the value should be 2" element, giving you something like:

context "the value" do
  subject{@value}
  it {should be(2)}
  it {should_not be(3)
end

However, I can't figure out how to add tags to that sort of test. Is this possible? Something like:

it :focus {should have_content("Oh yeah")}

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

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

发布评论

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

评论(2

浅浅淡淡 2024-12-26 03:45:10

你很接近,但 Ruby 不会解析它:focus => true { ... }。试试这个:

it(:focus => true) { should do_something }

You were close, but Ruby won't parse it :focus => true { ... }. Try this:

it(:focus => true) { should do_something }

海之角 2024-12-26 03:45:10

是的,这是可能的。试试吧!在执行之前,您可能需要先调用subject,{should have_content('Oh Yeah')}

Yes, this is possible. Try it! You may need to call subject before you do it {should have_content('Oh yeah')}.

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