2014-04-22 117 views
-1

我是新來測試,我需要在代碼文件中添加代碼到類中,以便在規範文件中創建的測試通過。RSpec的測試驅動開發

我已經在代碼文件中寫了一條評論,我認爲應該寫這些測試,但是如何編寫它們以便它們通過?

我的規格代碼如下所示:

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 

回答

0

TDD,你編寫測試首先爲你的代碼會來寫。正如我所看到的,您已經編寫了測試,現在您需要在您所評論的地方編寫實際的實現。您需要實現start函數,以便您的測試通過。

你可以閱讀有關TDDhere,並與RSpechereTDD

+0

你能給我一個啓動功能的例子嗎? – samgbelton

+0

首先,你沒有正確寫出'RSpec'測試,你應該將'start'函數的輸出響應與建議的輸出相匹配。當你爲這個功能的所有可能的輸出做了測試時,你可以開始執行它。 我會推薦你​​閱讀這個[鏈接](http://code.tutsplus.com/tutorials/ruby-for-newbies-testing-with-rspec-net-21297),這將有助於你理解這些概念。 – Saurabh