require 'rspec' require 'roll' describe Roll do let(:rand_generator) do rand_generator = double('rand_gen').as_null_object rand_generator.stub(:rand).and_return(1,2,3,4,5,6,7,8,9,10) rand_generator end before(:each) do unless Roll.respond_to?(:size) class Roll def size @dice.count end end end end describe "roll" do it "should have elements" do roll = Roll.new(2, rand_generator) roll.should have(2).items end it "returns a list of numbers" do roll = Roll.new(10, rand_generator) roll.should == [1,2,3,4,5,6,7,8,9,10] end it "calls Random once for each call" do Random.should_receive(:rand).exactly(5) roll = Roll.new(5) end it "calls Random with default value of 10 when no options provided" do Random.should_receive(:rand).with(10).exactly(5) roll = Roll.new(5) end end describe "reroll" do it "rerolls all dice that match the parameter" do roll = Roll.new(5, rand_generator) roll.reroll(3).should == [1,2,6,4,5] end it "rerolls all dice that match any of the parameters" do roll = Roll.new(5, rand_generator) roll.reroll(3,4).should == [1,2,6,7,5] end it "applies until it can't" do rand_generator = double('rand_gen').as_null_object rand_generator.stub(:rand).and_return(1,1,1,4) roll = Roll.new(1, rand_generator) roll.reroll(1).should == [4] end end describe "explode" do it "adds die to the result of all dice that match the parameter" do roll = Roll.new(5, rand_generator) roll.explode(3).should == [1,2,9,4,5] end it "adds die to the result of all dice that match any of the parameters" do roll = Roll.new(5, rand_generator) roll.explode(3,4).should == [1,2,9,11,5] end it "applies until it can't" do rand_generator = double('rand_gen').as_null_object rand_generator.stub(:rand).and_return(10,10,10,9) roll = Roll.new(1, rand_generator) roll.explode(10).should == [39] end end end