- 追加された行はこの色です。
- 削除された行はこの色です。
&tag(WebMock);
*目次 [#w5ecdb94]
#contents
*参考情報 [#p3abd201]
-[[bblimke/webmock · GitHub:https://github.com/bblimke/webmock]] // 公式
-[[[Gem] webmock RSpecのWebアクセスのモックを作成してくれる超便利Gem [Rails] - 酒と泪とRubyとRailsと:http://morizyun.github.io/blog/webmock-rspec-gem-ruby-rails/]]
*インストール [#vb033331]
-Gemfileを編集
#pre{{
group :test do
gem 'webmock'
end
}}
-インストール
bundle install
$ bundle install --path vendor/bundle
*サンプル [#g9fa72df]
-例えば以下のようなspecファイルでテストできる。stub_requestを使ってそのURLにたいするリクエストの応答を予め定義しておくことが可能。
#pre{{
# -*- coding: utf-8 -*-
require 'spec_helper'
require 'webmock/rspec'
require 'open-uri'
# 全てのリクエストを行わない
WebMock.disable_net_connect!
describe 'サンプル' do
before do
html = <<EOS
<html>
<head>
<title>テストです</title>
</head>
<body>
</body>
</html>
EOS
url = 'http://www.example.org/'
stub_request(:get, url).to_return({:body => html,:status => 200})
body = open(url).read
@title = nil
if body =~ /<title>([^<]+)</
@title = $1
end
end
it {@title.should == 'テストです'}
end
}}