Skip to content

Instantly share code, notes, and snippets.

@stevo
Created October 5, 2017 09:05
Show Gist options
  • Save stevo/bd7585e0a986fae589ef544ea02ab099 to your computer and use it in GitHub Desktop.
Save stevo/bd7585e0a986fae589ef544ea02ab099 to your computer and use it in GitHub Desktop.
Being explicit about data
# Before - use of described_class;
# exact values are visible only in setup and not in assertions;
# initial value is not known;
# values used are not realistic;
# irrelevant values are defined
describe UserForm do
describe "#save" do
it "persists changes" do
user = create(:user, age: 999)
attributes = { first_name: "abc123" }
form = described_class.new(user, attributes)
expect { form.save }.to change { user.reload.first_name }.to(attributes[:first_name])
end
end
end
# After - correlation between values is clearly visible;
# values are realistic
describe UserForm do
describe "#save" do
it "persists changes" do
user = create(:user, first_name: "John")
attributes = { first_name: "Tony" }
form = UserForm.new(user, attributes)
expect { form.save }.to change { user.reload.first_name }.from("John").to("Tony")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment