我只是想了解單元測試背後的基礎知識。我在一個名爲Player.rb的文件中寫了一個Player類。下面是代碼:在ruby中使用rspec進行首次單元測試
Class Player
attr_reader :health
attr_accessor :name
def initialize(name, health=100)
@name = name.capitalize
@health = health
end
def blam
@health -= 10
puts "#{@name} just got blammed yo."
end
def w00t
@health += 15
puts "#{@name} just got w00ted."
end
def score
@health + @name.length
end
def name=(new_name)
@name = new_name.capitalize
end
def to_s
puts "I'm #{@name} with a health of #{@health} and a score of #{score}"
end
end
這裏是我的spec文件:
require_relative 'Player'
describe Player do
it "has a capitalized name" do
player = Player.new("larry", 150)
player.name.should == "Larry"
end
end
確實似乎對吧?我的問題是關於spec文件的語法。我明白爲什麼我需要玩家類。但是描述的是和它代碼段的做法是什麼?爲什麼我需要它部分?所有它似乎在做的是定義一個字符串的權利?
最後,當我運行從終端rspec的player_spec.rb,我得到這樣的警告:
Deprecation Warnings:
Using `should` from rspec-expectations' old `:should` syntax without explicitly enabling the syntax is deprecated. Use the new `:expect` syntax or explicitly enable `:should` instead. Called from /Users/Jwan/studio_game/player_spec.rb:7:in `block (2 levels) in <top (required)>'.
是什麼上面的警告是什麼意思?我是否必須用替換以啓用語法?如何啓用:should語法?爲什麼是:應該寫成符號?
相當多。另外,'it'塊定義了您可以訪問RSpec期望匹配器的上下文,因此它不是純粹用於組織。上下文和描述將更多用於組織目的 – 2014-10-20 15:44:53