I’m on the last chapter of this excellent Rails tutorial and I’ve come upon this code:
describe "creating a relationship with Ajax" do
it "should increment the Relationship count" do
expect do
xhr :post, :create, relationship: { followed_id: other_user.id }
end.to change(Relationship, :count).by(1)
end
it "should respond with success" do
xhr :post, :create, relationship: { followed_id: other_user.id }
response.should be_success
end
end
I was wondering if there was any way to stop the repetition, similar to the following code (which won’t work as it only creates 2 tests)?
it "should increment relationships count" do
expect do
xhr :post, :create, relationship: { followed_id: other_user.id }
response.should be_success
end.to change(Relationship, :count).by(1)
end
This is not a DRY issue. You should separate incrementing test & success response