使用 mocks 和 doubles 进行测试被视为方法

testing with mocks and doubles being treated like methods

我在测试中引入了模拟和存根,一切顺利。我遇到了一些困难,因为自从更改代码以进一步测试后,我在 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)

第二个测试缺少一个 do 和 end 问题现在已解决!

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