Commits

Anonymous committed 9776f02

added rspec samples

  • Participants
  • Parent commits 57918af

Comments (0)

Files changed (3)

RSpec_samples/Gemfile

+source 'http://rubygems.org'
+
+gem 'rspec'

RSpec_samples/calculator.rb

+class Calculator
+
+  attr_reader :input_values, :last_result
+
+  def initialize
+    @input_values = []
+    @last_result = 0
+  end
+
+  def sum(x, y)
+    @input_values << x << y
+
+    @last_result = x + y
+    @last_result
+  end
+
+  def subtract(x, y)
+    @input_values << x << y
+
+    @last_result = x - y
+    @last_result
+  end
+end

RSpec_samples/spec/calculator_spec.rb

+require './calculator'
+
+#describe Calculator do
+#
+#  it "should sum two numbers" do
+#    calculator = Calculator.new
+#    result = calculator.sum(1, 1)
+#    result.should == 3
+#  end
+#
+#  it "should subtract two numbers" do
+#    calculator = Calculator.new
+#    result = calculator.subtract(5, 3)
+#    result.should == 2
+#  end
+#end
+
+#describe Calculator do
+#  context "Given a calculator" do
+#    before(:each) do
+#      @calculator = Calculator.new
+#    end
+#
+#    describe "when told to add 1 + 1" do
+#
+#      before { @calculator.sum(1, 1) }
+#
+#      it "should have two input values" do
+#        @calculator.input_values.length.should == 2
+#      end
+#
+#      it "should have last result of 2" do
+#        @calculator.last_result.should == 2
+#      end
+#    end
+#  end
+#end
+
+describe Calculator do
+  context "Given a calculator" do
+    subject { Calculator.new }
+
+    describe "when told to add 1 + 1" do
+
+      before { subject.sum(1, 1) }
+
+      it { should have(2).input_values }
+      its(:last_result) { should == 2 }
+    end
+
+    #describe "when told to subtract 5 - 3" do
+    #
+    #  before { @calculator.subtract(5, 3) }
+    #
+    #  it { should have(2).input_values }
+    #  its(:last_result) { should == 2 }
+    #end
+  end
+end