ホーム > 今日のテストコード > HTTP接続に依存した処理のテスト

HTTP接続に依存した処理のテスト

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. 2020 年 4 月 1 日 20:10 | #2

    your intelligence on just posting videos to your site when you

  2. 2020 年 4 月 1 日 21:21 | #3

    Thanks, I ave recently been seeking for facts about this subject matter for ages and yours is the best I ave located so far.

  3. 2020 年 4 月 1 日 22:48 | #8

    Wow! This could be one particular of the most useful blogs We ave ever arrive across on this subject. Basically Fantastic. I am also an expert in this topic so I can understand your hard work.

  4. 2020 年 4 月 2 日 00:04 | #9

    Thanks funny locate Loli Preteen I stroke sorry in support of the woman who had to shove a camera up her pussy and next retrieve the film.

  5. 2020 年 4 月 2 日 01:16 | #12

    Merely a smiling visitant here to share the love (:, btw great style and design.

  6. 2020 年 4 月 2 日 03:17 | #14

    or fashionable and useful, you will easily find your Id Nike Blazers sandals at a discount price to fit your budget.

  7. 2020 年 4 月 2 日 04:28 | #16

    such detailed about my trouble. You are incredible!

  8. 2020 年 4 月 2 日 04:41 | #18
  9. 2020 年 4 月 2 日 05:39 | #20

    You made some decent points there. I did a search on the topic and found most people will agree with your website.

  10. 2020 年 4 月 2 日 06:51 | #21

    With thanks! A good amount of information!

  11. 2020 年 4 月 2 日 08:19 | #25

    So happy to get found this submit.. Is not it terrific once you obtain a very good submit? Great views you possess here.. My web searches seem total.. thanks.

  12. 2020 年 4 月 2 日 08:42 | #26

    Spot on with this write-up, I really believe that this amazing site needs far more attention. I’ll probably be returning to read more, thanks for the info!

  13. 2020 年 4 月 2 日 09:32 | #28

    Everything is very open with a clear clarification of the issues. It was definitely informative. Your site is extremely helpful. Thank you for sharing!

  14. 2020 年 4 月 2 日 09:45 | #29

    I’m amazed, I must say. Seldom do I encounter a blog that’s both educative and amusing, and let me tell you, you’ve hit the nail on the head. The problem is something which not enough men and women are speaking intelligently about. I’m very happy I came across this during my search for something relating to this.

  15. 2020 年 4 月 2 日 10:15 | #31

    I couldn’t resist commenting. Very well written!

  16. 2020 年 4 月 2 日 10:18 | #32

    You have made some really good points there. I looked on the web to find out more about the issue and found most people will go along with your views on this web site.

コメントページ
1 18 19 20 328
  1. トラックバックはまだありません。