創建一個rspec測試以返回一個名稱

[英]Create an rspec test to return a name


I'm finding the whole TDD very confusing. I've passed the first two test successfully but I am not sure on how to write the last last one.

我發現整個TDD非常令人困惑。我成功通過了前兩個測試,但我不確定如何寫最后一個測試。

All it needs is to return the student's name. I've highlighted where it should go.

它所需要的只是返回學生的名字。我已經突出了它應該去的地方。

RSpec:

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

Current tests (first two tests work fine)

目前的測試(前兩個測試工作正常)

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 个解决方案

#1


0  

well you seem to need a game created_by field which you should add, you then need to set it when you create the game and then return it when you start the game, something like this:

好吧你似乎需要一個你應該添加的游戲created_by field,你需要在創建游戲時設置它,然后在你開始游戲時返回它,如下所示:

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

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2014/04/24/720a4f88d5f9ed54d374586d901a4380.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com