Created
March 13, 2011 22:10
-
-
Save r00k/868487 to your computer and use it in GitHub Desktop.
Which specs do you prefer?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Which specs do you prefer? | |
# | |
# These? | |
describe SalaryAverager do | |
before(:each) do | |
@averager = SalaryAverager.new("spec/fake_data.csv") | |
end | |
describe "#average_salary" do | |
it "returns the average salary for all entries" do | |
@averager.average_salary.should == 110000.0 | |
end | |
end | |
describe "salaries" do | |
it "returns an array of salary floats" do | |
@averager.salaries.should == [100000.0, 120000.0] | |
end | |
end | |
end | |
# Or these? | |
describe SalaryAverager do | |
before(:each) do | |
@averager = SalaryAverager.new("spec/fake_data.csv") | |
end | |
describe "#average_salary" do | |
specify { @averager.average_salary.should == 110000.0 } | |
end | |
describe "salaries" do | |
specify { @averager.salaries.should == [100000.0, 120000.0] } | |
end | |
end |
If methods as straightforward as these, I prefer another rspec's 'its' style:
describe SalaryAverager do
subject { SalaryAverager.new("spec/fake_data.csv") }
its(:average_salary) { should == 110000.0 }
its(:salaries) { should == [100000.0, 120000.0] }
end
What do you think?
It boils down to whether you care about the output or not. The first ones will output this:
SalaryAverager #average_salary returns the average salary for all entries salaries returns an array of salary floats
This ^^ tells me what the object does in an abstract way. The latter examples produce this, which is arguably useless as documentation:
SalaryAverager #average_salary should == 110000.0 salaries should == [100000.0, 120000.0]
David: thanks for the clarification. Do you use the documentation output often? I know some folks run their specs like that all the time, but I never have.
I actually think the first version wins over the second even without this output difference. The "docstrings" provide a lot of useful info in my experience.
r00k - absolutely!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Personally, I prefer the "docstring" style of the first example. However, for methods as straightforward as these, I appreciate the second's terseness.