QRコードの生成にGoogle Chart APIを使ってみました。
rspec-railsのhave_tagマッチャを使うときはassert_selectのドキュメントを見ながらの場合が多いですね。早く覚えてしまいたい。

app/helpers/chart_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| module ChartHelper
def qr_code_tag(text, options = {})
param = {
:cht => 'qr',
:chl => text
}
param[:chs] = options.delete(:chart_size) || '120x120'
uri = URI::HTTP.build(:host => 'chart.apis.google.com', :path => '/chart', :query => param.to_query)
image_tag uri.to_s, options
end
end |
spec/helpers/chart_helper_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| require 'spec_helper'
describe ChartHelper do
it '指定したテキストを含むQRコードを得られること' do
tag = helper.qr_code_tag('http://tech.hapicky.com/')
expected_src = 'http://chart.apis.google.com/chart?chl=http%3A%2F%2Ftech.hapicky.com%2F&chs=120x120&cht=qr'
tag.should have_tag('img[src=?]', expected_src)
end
it 'サイズ指定できること' do
tag = helper.qr_code_tag('http://tech.hapicky.com/', :chart_size => '300x300')
expected_src = 'http://chart.apis.google.com/chart?chl=http%3A%2F%2Ftech.hapicky.com%2F&chs=300x300&cht=qr'
tag.should have_tag('img[src=?]', expected_src)
end
it 'altの指定ができること' do
tag = helper.qr_code_tag('http://tech.hapicky.com/', :alt => 'tech-memo')
expected_src = 'http://chart.apis.google.com/chart?chl=http%3A%2F%2Ftech.hapicky.com%2F&chs=120x120&cht=qr'
tag.should have_tag('img[src=?][alt=tech-memo]', expected_src)
end
end |
参考:
rspecのincludeマッチャ。Hashの場合の挙動はバージョン間で色々と違いがありますね。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
| require 'rubygems'
gem 'rspec', '>= 1.1.12'
require 'spec'
describe 'includeマッチャ' do
before :each do
@lang_authors = {
:ruby => 'まつもとゆきひろ',
:python => 'Guido van Rossum',
:perl => 'Larry Wall'
}
end
it 'Hashが指定されたキーを持つか' do
# 以下はrspec 1.1.10, 1.1.11 でエラーになる
@lang_authors.should include(:ruby)
@lang_authors.should include(:python)
@lang_authors.should_not include(:vb)
end
it 'Hashが指定されたキーと値の組み合わせを持つか' do
# 以下はrspec 1.1.10より前だと失敗する
@lang_authors.should include(:ruby => 'まつもとゆきひろ')
@lang_authors.should_not include(:ruby => 'まつもとひろゆき')
end
it 'その他はinclude?による評価' do
@lang_authors[:python].should include('os')
@lang_authors.keys.should include(:perl)
end
end |
参考:
HTTP接続に依存した処理をテスト。以下ではwebmockを使っていますが他にもfakewebなどありますね。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
| require 'rubygems'
require 'spec'
require 'webmock/rspec'
require 'open-uri'
class HttpClient
def initialize(url)
@url = url
end
def fetch_body
begin
open(@url) { |io| io.read }
rescue OpenURI::HTTPError
nil
end
end
end
describe HttpClient do
TEST_URL = 'http://www.example.com/test'
before :each do
@client = HttpClient.new(TEST_URL)
end
it 'ステータスコード200の場合はbodyの内容を返すこと' do
body = "OK"
WebMock.stub_request(:get, TEST_URL).to_return(:body => body, :status => 200)
@client.fetch_body.should == body
end
it '接続できない場合はnilを返すこと' do
body = "NG!"
WebMock.stub_request(:get, TEST_URL).to_return(:body => body, :status => 404)
@client.fetch_body.should be_nil
end
end |
参考:
テスト中にファイル書き込みを行いたいのでテンポラリディレクトリを利用する。終わったら掃除。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| require 'rubygems'
require 'spec'
require 'tmpdir'
describe 'テンポラリディレクトリを使ったテスト' do
before :all do
@tmp = Dir.mktmpdir
end
it 'なんらかのテスト' do
# @tmp に書き込みを行うテストなど
end
after :all do
FileUtils.remove_entry_secure @tmp if File.exist?(@tmp)
end
end |
参考: