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
函数的输出响应与建议的输出匹配。当您为这个函数的所有可能输出编写了测试之后,就可以开始实现它了。我建议您阅读本文,这将有助于您理解这些概念。