使用RSpec进行测试驱动开发

时间:2014-04-22 12:08:25

标签: ruby rspec tdd

我是测试的新手,我需要在代码文件中向类中添加代码,以便在规范文件中创建的测试通过。

我在代码文件中写了一条评论,我认为应该编写测试,但是如何编写它们以便它们通过?

我的规范代码如下:

require "wad_TIM_00_gen"

module ImpossibleMachine
    # Input and output constants processed by subprocesses
    DOWN_ARROW = 1
    UP_ARROW = 2
    RIGHT_ARROW = 3
    REPEAT_ARROW = 4
    END_PROCESS = 5 
    START_CURRENT = 6

    # RSpec Tests 
    describe Game do
        describe "#start The impossible machine game" do
            before(:each) do
                @process = []
                @output = double('output').as_null_object
                @game = Game.new(@output)
            end

            it "sends a welcome message" do
                @output.should_receive(:puts).with('Welcome to the Impossible Machine!')
                @game.start
            end

            it "sends a starting message" do
                @output.should_receive(:puts).with('Starting game...')
                @game.start         
            end
        end
    end

我的代码文件如下所示:

# Main class module
module ImpossibleMachine
    # Input and output constants processed by subprocesses. MUST NOT change.
    DOWN_ARROW = 1
    UP_ARROW = 2
    RIGHT_ARROW = 3
    REPEAT_ARROW = 4
    END_PROCESS = 5 
    START_CURRENT = 6

    class Game
        attr_reader :process, :output
        attr_writer :process, :output

        def initialize(output)
            @output = output
            puts "[#{@output}]"
        end

        # I think the tests should be written here

    end
end

1 个答案:

答案 0 :(得分:0)

TDD中,首先为要编写的代码编写测试。正如我所看到的,您已经编写了测试,现在您需要在您评论的位置编写实际的实现。您需要实现start函数,以便您的测试通过。

您可以通过TDD here了解TDD hereRSpec