0

私は FactoryGirl を使用して、ユニット (RSpec) テストと統合 (Cucumber) テストの両方でモデルをモックしています。ファクトリを作成するときNews、ランダムな画像 URL を作成しますが、これは明らかにプロジェクト内には存在しません。Selenium テストを実行すると、これは 404 として取得されます。

FactoryGirl.define do
  factory :news do
    title { Faker::Lorem.sentence }
    image { Faker::Internet.relative_url ".jpg" }
    body { Faker::Lorem.paragraphs.join "\r\n\r\n" }

    before :create do
      Timecop.freeze Faker::Date.time
    end

    after :create do
      Timecop.return
    end

    after :build do |article|
      # Somehow mock a 200 response for #{article.image}
    end

    factory :published_news do
      published true
    end
  end
end

画像の応答をモックする最良の方法は何ですか?

4

3 に答える 3

0

仕様に次のものを追加しただけです:

describe Zomg do
  it 'should get ok' do 
    stub_request(:any, "http://factory-generated-url-here/").to_return(body: '', status: 200)

   # you stuff with request        
  end   
end

ありがとう_

于 2013-10-05T16:52:57.333 に答える
0

200を返したいなら、このまま返せませんか?

after :build do |article|
 ["200", "OK"]
end
于 2013-10-05T16:25:56.203 に答える