1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ウェブからの画像取得処理をテスト内で用いる

Last updated at Posted at 2013-06-06

例として、Googleのロゴ画像の取得をテストします。
画像は該当のURL( http://www.google.com/images/logos/ps_logo2.png )から予め手動で取得しspec/support/filesの下に置いておきます。

test_spec.rb
context "ウェブから画像を取得する時" do 
  before do
     @url = "http://www.google.com/images/logos/ps_logo2.png"
     path = Rails.root.join("spec/support/files", "ps_logo2.png")
     FakeWeb.register_uri(:get, @url,
                          body: Rack::Test::UploadedFile.new(path),
                          content_type: "image/png")
  end
  # 以下略

こうしておけば、テストの中で通常のコードで使うのと同じ形で@urlを参照できます。
bodyにRack::Test::UploadedFile.new(path)を渡すのが肝ですね。

参考:Stub a image url and return an image

P.S.
以下のgemが必要になります。

$ gem install rack-test
$ gem install fakeweb
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?