Skip to content

Instantly share code, notes, and snippets.

@r00k
Created March 13, 2011 22:10
Show Gist options
  • Save r00k/868487 to your computer and use it in GitHub Desktop.
Save r00k/868487 to your computer and use it in GitHub Desktop.
Which specs do you prefer?
# 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
@dchelimsky
Copy link

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]

@r00k
Copy link
Author

r00k commented Mar 14, 2011

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.

@dchelimsky
Copy link

r00k - absolutely!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment