【问题标题】:Test Driven Development with RSpec使用 RSpec 进行测试驱动开发
【发布时间】:2014-04-22 12:08:25
【问题描述】:

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

我在我认为应该编写测试的代码文件中写了一条注释,但我该如何编写它们才能通过?

我的规范代码如下所示:

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

【问题讨论】:

    标签: ruby rspec tdd


    【解决方案1】:

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

    您可以阅读有关TDD here 的信息,以及有关TDDRSpec here 的信息。

    【讨论】:

    • 能否给我一个启动函数的例子?
    • 首先,您没有正确编写RSpec 测试,您应该将start 函数的输出响应与建议的输出相匹配。当你为这个函数的所有可能输出编写了测试后,你就可以开始实现它了。我会推荐你​​阅读这个link,它将帮助你理解这些概念。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-23
    • 2015-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-19
    相关资源
    最近更新 更多