How to keep rspec tests DRY with lots of “have_link”

99封情书 提交于 2019-12-05 09:47:47

To define custom matchers you can read this feature and some inspiration.

And write something like that:

RSpec::Matchers.define :have_links do |expected|
  match do |actual|
    expected.all? do |name, options|
      have_link(name, options).matches?(actual)
    end
  end
end

But IMO, your first try is the best way to write it: clean and easy to read.

You can write a custom matcher, but I think thats not the idea of tests and DRY.

In the code, the DRY mantra encourages to keep every piece of knowledge of your software in a unique and unambiguous place. That is not the goal of the specs. The goal of the specs is to poof the correctness of a software in a explicit and easy to read way.

Repeat

it { should have_link('Users', href: users_path) }

if far more readable and easy to read than declaring and array of [text, url] and iterate over them, even inside some kind of custom matcher.

In test you should prefer readability over conciseness.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!