Am I on the right track?

Hi,

I am pretty new to RSpec and I wonder if some code I have written is
leading me in the right direction. Any advice is greatly appreciated!

here is the code from calculator.rb

class Calculator
def equation(val1, val2)
result = val1 + val2
return result
end
end

and here is the code from my calculator_spec.rb

require ‘calculator’

describe Calculator do
it “should perform calculations” do
calc = Calculator.new
result = calc.equation(1, 5)
result.should == 6
end
end

On Mon, Sep 21, 2009 at 5:48 PM, Calvin [email protected] wrote:

I am pretty new to RSpec and I wonder if some code I have written is
leading me in the right direction. Any advice is greatly appreciated!

As a beginner exercise, this code looks perfectly functional to me.
But I believe you left out the interesting parts:

  • Which did you write first? The spec or the implementation?

  • Is the behavior description, “it should perform calculations,” clear
    and precise? If I didn’t have access to the code, would I be able to
    read that description and know something useful about the class’s
    behavior?

  • Why did you choose the method name “equation”? If I simply knew
    that name, would I be able to tell what that method does?

  • What is it you really want this class to do? Does the
    specification help you verify that it acts according to your
    intention?

  • Why do you want it to do that? (Okay, granted, this may be more of
    a Cucumber question than an RSpec question.) >8->


Have Fun,
Steve E. ([email protected])
ESCAPE POD - The Science Fiction Podcast Magazine
http://www.escapepod.org

Hi Steve,

I wrote the spec first and then the implementation. I think I could
have written a better behavior description. I don’t think the method
name is as good or specific as it could be. I just wanted the class to
be able to add two numbers… so maybe the specification could reflect
that more… and I want the class to add two numbers because I wanted
to practice using rspec and figured a simple program would be good to
start out with.

  • Calvin