B Seven
B Seven

Reputation: 45941

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...

Upvotes: 9

Views: 6143

Answers (2)

doesterr
doesterr

Reputation: 3965

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

Upvotes: 6

Gregory Brown
Gregory Brown

Reputation: 1378

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.

Upvotes: 15

Related Questions