我只是想了解单元测试背后的基础知识。我在一个名为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