I have the following rspec request spec example which passes
it "increases count by 1" do
attributes = attributes_for(:district)
expect { post admin_districts_path, params: { district: attributes} }.to change { District.count }.by(1)
end
The expect line is a little busy, so I'm trying to break it up. The following causes an error
it "increases count by 1" do
attributes = attributes_for(:district)
block = { post admin_districts_path, params: { district: attributes} }
expect(block).to change { District.count }.by(1)
end
with error
syntax error, unexpected '}', expecting keyword_end
Why is this error happening? Is there a cleaner way to write this spec example?
I usually run into this kind of long lines in tests. Instead of creating new variables just to improve reading, what I do is to split it into different lines like this:
it "increases count by 1" do
attributes = attributes_for(:district)
expect do
post admin_districts_path, params: { district: attributes}
end.to change { District.count }.by(1)
end
Also, you can create a lambda:
block = -> { post admin_districts_path, params: { district: attributes} }
expect(block).to change { District.count }.by(1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With