听闻过TDD有一些时日了,加上之前看过的《敏捷开发的艺术》,是时候试着用这种方法来写一个程序。于是就谷歌到了ruby rspec,同是敏捷开发,但是却是BDD——行为驱动开发。
来自维基百科的简述
BDD的做法包括:
手头上的工具及书籍包含以下(转载保留 Phodal's Blog Phodal's zenthink) - 《Ruby元编程》 - 《Programming Ruby》 -《重构:改善既有代码的设计》 - Mint Linux以及OpenSUSE Linux -《测试驱动开发的艺术》
官方的介绍如下
> RSpec is testing tool for the Ruby programming language. Born under the banner of Behaviour-Driven Development, it is designed to make Test-Driven Development a productive and enjoyable experience with features like: > - a rich command line program (the rspec command) - textual descriptions of examples and groups (rspec-core) - flexible and customizable reporting - extensible expectation language (rspec-expectations) - built-in mocking/stubbing framework (rspec-mocks)
简单的翻译一下就是
RSpec是Ruby编程语言的测试工具。在行为驱动开发的旗帜下诞生的,它的目的是使测试驱动开发一个富有成效和愉快的体验与功能,如:
安装,理论上是可以直接用
sudo apt-get install ruby-rspec
实际上我觉得还是用 gem install rspec
因为直接用Mint Linux上面的命令带来另外两个版本的ruby,最后在安装rails的时候,问题终于爆发了。
这部分参考了这里的文章 使用 RSpec 进行行为驱动测试 这里讲到的思想和TDD很像,也就是不断地通过测试。
添加任务到Rakefile
require 'rspec/core/rake_task'
task :default => :spec
desc "run all examples."
RSpec::Core::RakeTask.new(:spec)
创建目录spec mkdir spec gedit spec/spec_helper.rb
用来集中使用到的库 添加一个简单的类的测试
require 'spec_helper'
describe Hotel do
let(:hotel){Hotel.new("Photel",3,110)}
it "should return countcost of final" do
hotel.name.should be_a String
end
end
创建一个简单的类来通过测试
class Hotel
def initialize(name,rating,price)
@name=name
@rating=rating
@price=price
end
end
rake
这样的迭代过程用来开发程序算是一件不错的事。
这个算是我觉得Ruby比python 强大的另外一个吧,还有一个是OO的出神入化。 不过用makefile的话,也是不错的,对于python来说。 至于Ruby还是Rake,不过Rake也可以用于其他场合的构建。
围观我的Github Idea墙, 也许,你会遇到心仪的项目