创建一个rspec测试以返回一个名称

时间:2014-04-24 09:27:34

标签: ruby rspec tdd

我发现整个TDD非常令人困惑。我成功通过了前两个测试,但我不确定如何写最后一个测试。

所需要的只是返回学生的名字。我已经强调了应该去哪里。

RSpec的:

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
        it "should contain a method created_by which returns the students name" do
            myname = @game.created_by
            myname.should == "Student's name"
        end
    end
end

当前测试(前两个测试工作正常)

class Game
    attr_reader :process, :output
    attr_writer :process, :output
    def initialize(output)
        @output = output
        puts "[#{@output}]"
    end
    def start
        @output.puts'Welcome to the Impossible Machine!'
        @output.puts'Starting game...'
        # code to return student's name here
    end
end

1 个答案:

答案 0 :(得分:0)

你似乎需要一个你应该添加的游戏created_by字段,然后你需要在创建游戏时设置它,然后在你开始游戏时返回它,如下所示:

class Game
    attr_accessor :created_by
    attr_reader :process, :output
    attr_writer :process, :output
    def initialize(output, created_by)
        @output = output
        @created_by = created_by
        puts "[#{@output}]"
    end
    def start
        @output.puts 'Welcome to the Impossible Machine!'
        @output.puts 'Starting game...'
        @output.puts @created_by
        # code to return student's name here
    end
end