RSpec-Rails (基礎篇)(1): 基本用法
概要
- 如何安裝RSpec
- 如何用單一檔案執行RSpec
- describe及it用法簡介
- 兩個使用案例
參考資料
筆記
版本
Ruby版本: 2.1.2
RSpec版本: 3.2.0
安裝
$ gem install rspec
$ gem install httpclient
簡易數學
require 'rspec'
describe "Math" do
it 'do basic math' do
expect(1+1).to eq(2)
end
end
執行
$ rspec basic_spec.rb
薪水分類
require 'rspec'
class Divider
def salary_divide(salary)
case salary
when 0..300000
"年收入30萬以下"
when 300000..600000
"年收入30~60萬之間"
when 600000..900000
"年收入60~90萬之間"
else
"年收入90萬以上"
end
end
end
describe "salary" do
it 'is categorized successfully based on input' do
expect(Divider.new.salary_divide(250000)).to eq("年收入30萬以下")
expect(Divider.new.salary_divide(290000)).to eq("年收入30萬以下")
expect(Divider.new.salary_divide(358000)).to eq("年收入30~60萬之間")
expect(Divider.new.salary_divide(540000)).to eq("年收入30~60萬之間")
expect(Divider.new.salary_divide(744000)).to eq("年收入60~90萬之間")
expect(Divider.new.salary_divide(950000)).to eq("年收入90萬以上")
end
end
確認連線正常
require 'rspec'
require 'httpclient'
describe "connections" do
it 'are stable' do
expect(HTTPClient.new.get("http://rspec.info/").status).to eq 200
expect(HTTPClient.new.get("http://rspec.info/").body).to include("Behaviour Driven Development in Ruby with RSpec")
expect(HTTPClient.new.get("https://evernote.com/intl/zh-tw/").status).to eq 200
expect(HTTPClient.new.get("https://www.linode.com/").status).to eq 200
end
end