环境:
ruby 1.9.3
rails 3.1.3
简介:
RSpec is a Behaviour-Driven Development tool for Ruby programmers. BDD is an approach to software development that combines Test-Driven Development, Domain Driven Design, and Acceptance Test-Driven Planning. RSpec helps you do the TDD part of that equation, focusing on the documentation and design aspects of TDD.
RSpec是ruby程序设计中行为驱动测试(BDD)的工具。(BDD)是一种结合测试驱动开发,领域驱动设计,验收测试的软件开发方法。rspec更侧重于测试驱动开发中文档和设计部分。
rails默认使用test/unit做为测试框架,那么如何使用Rspec呢?
1. 安装RSpec相关的Gem包
?
? ??gem install rspec-rails
?? ? 执行后,会自动安装相应的gem包:
?
rspec rspec-core rspec-expectations rspec-mocks rspec-rails
?
2. 新建一个rails工程,打开Gemfile文件,在roup中增加如下代码,注意添加:development,这段代码告诉Bundler我们需要在Rails中安装rspec-rails来运行RSpec
?
?
group :test, :development do # Pretty printed test output gem 'rspec-rails', '2.7.0' gem 'turn', '~> 0.8.3', :require => false end
?
3.?最后,运行RSpec的生成器
?
rails generate rspec:install
?
?
生成器创建了几个新文件,分别是:
F:\test_rspec>rails generate controller Pages contact
...
invoke rspec
create spec/controllers/pages_controller_spec.rb
create spec/views/pages
create spec/views/pages/contact.html.erb_spec.rb
....