Eisen's Blog

© 2023. All rights reserved.

rspec get started

October 04, 2013

rubytest

rspec

init

gem install rspec
rspec --init

.rspec

我们可以用 .rspec 文件做 rspec 的配置。

--color
--format documentation

这里的 documentation 可以让 rspec 显示出具体的信息,而默认的是 --format progress 只会显示类似 testunit 的显示形式。

rspec with rails

rails new *application* --skip-test-unit

可以让 rails 不要给予 test-unit 的任何内容。 然后在 Gemfile 添加

group :development, :test do
  gem 'rspec-rails'
end

然后

rails generate rspec:install

之后如果执行rails g scaffold那么就会在spec目录下建立相应的测试。

rspec with guard

group :development, :test do
  gem 'rspec-rails'
  gem 'rb-fsevent', :require => false if RUBY_PLATFORM =~ /darwin/i
  gem 'guard-rspec'
  gem 'guard-livereload'
end

安装 guard 的支持非常的简单。然后

guard init rspec
guard init livereload
guard

详情可见 railscasts #264 Guard

pending

对于还没有想好测试内容的情况这个就是超级有用了。

首先可以这样做

describe Hotel do
  it "should have method price"
end

或者这样

describe Hotel do
  it "should have method price" do
    pending
  end
end

一大堆的 matcher

zombie = Zombie.new (name: 'Ash')

zombie.name.should == 'Ash'
zombie.should respond_to(:name)
zombie.weapons.should include(weapon)
zombie.weapon.should_not be_nil