如何让open-uri在rails项目中工作(由新手询问)

时间:2015-12-08 14:16:18

标签: ruby-on-rails ruby rspec

我的规格:

require 'spec_helper'

RSpec.describe Category, type: :model do
  before { @category = FactoryGirl.build(:category) }
  subject { @category }
  ...

我的工厂:

FactoryGirl.define do
  factory :category do
    title { FFaker::Lorem.word }
    picture { FFaker::Avatar.image }
  end
end

我的宝石文件:

source 'https://rubygems.org'

gem 'rails', '4.2.4'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'sdoc', '~> 0.4.0', group: :doc
gem "devise"
gem 'font-awesome-rails'
gem 'simple_form'
gem 'sabisu_rails', github: "IcaliaLabs/sabisu-rails"
gem 'furatto', github: "IcaliaLabs/furatto-rails"
gem 'active_model_serializers', github: 'rails-api/active_model_serializers', branch: '0-8-stable'
gem "paperclip", "~> 4.3"
gem 'open_uri_redirections'

group :development, :test do
  gem 'byebug'
  gem 'factory_girl_rails'
  gem 'ffaker'
end

group :development do
  gem 'web-console', '~> 2.0'
  gem 'spring'
  gem 'sqlite3'

group :test do
  gem 'rspec-rails'
  gem 'shoulda-matchers', '2.5.0', require: false
  gem 'rspec-collection_matchers'
end

end 

我运行此命令=>

bundle exec rspec spec/models/category_spec.rb is :

Randomized with seed 44232 FFFFF Failures: 

1) Category 
 Failure/Error: before { @category = FactoryGirl.build(:category) }

 RuntimeError: redirection forbidden: robohash.org/debitismagnamodio.png?size=300x300 -> robohash.org/debitismagnamodio.png?size=300x300

工厂无法创建对象,因为它调用了FFake :: Avatar.image这是一个网址(我想,我不确定)。我已经在工厂和规范中都要求open-uri和open_uri_redirections,但它不起作用。

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

试试这个Gemfile

gem'open_uri_redirections',git:'git@github.com:open-uri-redirections / open_uri_redirections.git'

后 捆绑更新open_uri_redirections