用模拟和双打测试就像方法一样

时间:2015-01-20 18:07:11

标签: ruby rspec mocking stubs

我已经在我的测试中引入了模拟和存根,一切进展顺利。我打了一个墙,因为更改代码进一步测试我在rspec中得到以下错误但我无法理解为什么它会出现错误。当我刚刚测试它应该低于16时一切都很好,但当我介绍测试它应该达到16以上我得到以下错误。

undefined method `double' for RSpec::ExampleGroups::Hand::PlayAsDealer:Class (NoMethodError)

有问题的代码如下:

describe "#play_as_dealer" do 
        it "should hit below 16" do 
            deck = double(:deck, :cards => [Card.new(:clubs, 4), Card.new(:diamonds, 4), Card.new(:clubs, 2), Card.new(:hearts, 6)])
            hand = Hand.new
            2.times { hand.hit!(deck) }
            hand.play_as_dealer(deck)
            expect(hand.value).to eq(16)
        end

        it "should hit above 16"
            deck = double(:deck, :cards => [Card.new(:clubs, 4), Card.new(:diamonds, 4)])
            hand = Hand.new
            2.times { hand.hit!(deck) }
            hand.play_as_dealer(deck)
            expect(hand.value).to eq(17)

1 个答案:

答案 0 :(得分:0)

第二次测试错过了do和end-problem现在已经解决了!

it "should hit above 16" do 
            deck = double(:deck, :cards => [Card.new(:clubs, 4), Card.new(:diamonds, 4)])
            hand = Hand.new
            2.times { hand.hit!(deck) }
            hand.play_as_dealer(deck)
            expect(hand.value).to eq(17)
        end