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

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -