Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format multiline RSpec expect {}.to change

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

expect do...end works, but is even uglier...

like image 819
B Seven Avatar asked Nov 07 '12 17:11

B Seven


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 exactly pretty, but reduces some of the line noise.

like image 151
Gregory Brown Avatar answered Nov 16 '22 02:11

Gregory Brown


Since putting everything in curly braces and on one line would be too long, I'd write it like this:

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 :)

like image 20
doesterr Avatar answered Nov 16 '22 03:11

doesterr