How to format multi-line RSpec expect {}. Edit

Is there a better way to format this test to make it more readable?

expect { within '.foo' do click_link 'Delete' end }.to change {Foo.count}.by 1 

Waiting for do...end works, but even uglier ...

+4
source share
2 answers

Maybe something like this?

 expected = expect do within '.foo' do click_link 'Delete' end end expected.to change { Foo.count }.by 1 

Not entirely beautiful, but reduces some line noise.

+4
source

Since putting everything in curly braces and on one line would be too long, I would write it as follows:

 expect do within(".foo") { click_link "Delete" } end.to change { Foo.count }.by 1 

Update: Not tested, but this should work too:

 click_delete_link = lambda { within(".foo") { click_link "Delete" } } expect { click_delete_link }.to change { Foo.count }.by 1 

But I still like the first version better :)

+4
source

Source: https://habr.com/ru/post/1444626/


All Articles