ruby - DRY code using Rspec matchers -
i want improve following block of code more readable/concise/dry (my actual arguments bit more complicated #have_css):
negate ? expect(page).to(have_css('selector', text: 'text')): expect(page).to_not(have_css('selector', text: 'text')) is there anyway store 'selector', text: 'text' in variable reused method arguments?
or alternatively there special trick call correct matcher based on negate boolean value? perhaps similar
expectation = negate ? :should_not : :should page.send(expectation, have_css('selector', text: 'text'))` but using new rspec expect syntax?
you can store arguments used more once in array, splat (*) them argument list:
css_args = ['selector', {text: 'text'}] expect(page).to(have_css(*css_args)) you can pick matcher negate so:
expectation = negate ? :to_not : :to expect(page).send(expectation, have_css('selector', text: 'text'))
Comments
Post a Comment