Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 使用RSpec的测试驱动开发_Ruby_Rspec_Tdd - Fatal编程技术网

Ruby 使用RSpec的测试驱动开发

Ruby 使用RSpec的测试驱动开发,ruby,rspec,tdd,Ruby,Rspec,Tdd,我是测试新手,我需要向代码文件中的类添加代码,以便在规范文件中创建的测试能够通过 我在代码文件中写了一条注释,我认为应该在这里编写测试,但是我如何编写它们以使它们通过呢 我的规范代码如下所示: require "wad_TIM_00_gen" module ImpossibleMachine # Input and output constants processed by subprocesses DOWN_ARROW = 1 UP_ARROW = 2 RIG

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

我在代码文件中写了一条注释,我认为应该在这里编写测试,但是我如何编写它们以使它们通过呢

我的规范代码如下所示:

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
我的代码文件如下所示:

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

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


您可以阅读关于
TDD
,以及关于
TDD
RSpec

的内容,您可以给我一个启动函数的示例吗?首先,您没有正确编写
RSpec
测试,您应该将
start
函数的输出响应与建议的输出匹配。当您为这个函数的所有可能输出编写了测试之后,就可以开始实现它了。我建议您阅读本文,这将有助于您理解这些概念。