How to define an array / hash in factory_bot?

Ruby on-Rails-3RspecFactory Bot

Ruby on-Rails-3 Problem Overview


I am trying to write a test that simulates some return values from Dropbox's REST service that gives me back data in an Array, with a nested hash.

I am having trouble figuring out how to code my Factory since the return result is an array with a has inside. What would go here?

Factory.define :dropbox_hash do
 ??
end

Dropbox data looks like this:

 ["/home", {"revision"=>48, "rev"=>"30054214dc", "thumb_exists"=>false, "bytes"=>0, "modified"=>"Thu, 29 Dec 2011 01:53:26 +0000", "path"=>"/Home", "is_dir"=>true, "icon"=>"folder_app", "root"=>"app_folder", "size"=>"0 bytes"}] 

And I'd like a factory call like this in my RSpec:

Factory.create(:dropbox_hash)

Ruby on-Rails-3 Solutions


Solution 1 - Ruby on-Rails-3

I was interested in doing the same thing, also to test a model of mine that operates using a hash of content from a 3rd-party API. I found that by using a few of the built-in features of factory_girl I was able to cleanly construct these sort of data structures.

Here's a contrived example:

  factory :chicken, class:Hash do
    name "Sebastian"
    colors ["white", "orange"]

    favorites {{
      "PETC" => "http://www.petc.org"
    }}
    
    initialize_with { attributes } 
  end

The main trick here is that when you declare initialize_with, factory_girl will no longer attempt to assign the attributes to the resultant object. It also seems to skip the db store in this case. So, instead of constructing anything complicated, we just pass back the already prepared attribute hash as our content. Voila.

It does seem necessary to specify some value for the class, despite it not actually being used. This is to prevent factory_girl from attempting to instantiate a class based on the factory name. I've chosen to use descriptive classes rather than Object, but it's up to you.

You're still able to override fields when you use one of these hash factories:

chick = FactoryGirl.build(:chicken, name:"Charles")

..however, if you have nested content and want to override deeper fields you will need to increase the complexity of the initialization block to do some sort of deep merge.

In your case, you're using some mixed array and hash data, and it appears that the Path property should be reused between portions of the data structure. No problem - you know the structure of the content, so you can easy create a factory that constructs the resulting array properly. Here's how I might do it:

  factory :dropbox_hash, class:Array do
    path "/home"
    revision 48
    rev "30054214dc"
    thumb_exists false
    bytes 0
    modified { 3.days.ago }
    is_dir true
    icon "folder_app"
    root "app_folder"
    size "0 bytes"

    initialize_with { [ attributes[:path], attributes ] }
  end

  FactoryGirl.build(:dropbox_hash, path:"/Chickens", is_dir:false)

You are also still free to omit unnecessary values. Let's imagine only Path and rev are really necessary:

  factory :dropbox_hash, class:Array do
    path "/home"
    rev "30054214dc"
    initialize_with { [ attributes[:path], attributes ] }
  end

  FactoryGirl.build(:dropbox_hash, path:"/Chickens", revision:99, modified:Time.now)

Solution 2 - Ruby on-Rails-3

got this working for me, and i can pass attributes as needed into the hash

factory :some_name, class:Hash do
  defaults = {
    foo: "bar",
    baz: "baff"
  }
  initialize_with{ defaults.merge(attributes) }
end

> build :some_name, foo: "foobar" #will give you
> { foo: "foobar", baz: "baff" }

Solution 3 - Ruby on-Rails-3

A followup for the current RSpec version (3.0):

Just define your factory as usual and use FactoryBot.attributes_for to receive a hash instead of an instantiated class.

Solution 4 - Ruby on-Rails-3

You can do this in the latest versions of factory_girl, but it's awkward because it's designed to build objects and not data structures. Here's an example:

FactoryGirl.define do
  factory :dropbox_hash, :class => 'Object' do
    ignore do
      url { "/home" }
      revision { 48 }
      rev { "30054214dc" }
      # more attributes
    end
    initialize_with { [url, { "revision" => revision, "rev" => rev, ... }] }
    to_create {}
  end
end

Going over the weird stuff here:

  • Every factory needs a valid build class even if it's not used, so I passed Object here to prevent it from looking for DropboxHash.
  • You need to ignore all the attributes using an ignore block so that it doesn't try to assign them to the array afterwards, like array.revision = 48.
  • You can tell it how to put your result together using initialize_with. The downside here is that you need to write out the full attribute list again.
  • You need to provide an empty to_create block so that it doesn't try to call array.save! afterwards.

Solution 5 - Ruby on-Rails-3

I used OpenStruct:

factory :factory_hash, class:OpenStruct do
  foo "bar"
  si "flar"
end

Edit: sorry, does not work as an Hash

I finally use a static version, just to keep that hash coming from the Factory system...

factory :factory_hash, class:Hash do
  initialize_with { {
    foo "bar"
    si "flar"
  } }
end

looking for something better

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDougView Question on Stackoverflow
Solution 1 - Ruby on-Rails-3doublemarkedView Answer on Stackoverflow
Solution 2 - Ruby on-Rails-3jethrooView Answer on Stackoverflow
Solution 3 - Ruby on-Rails-3milgnerView Answer on Stackoverflow
Solution 4 - Ruby on-Rails-3Joe FerrisView Answer on Stackoverflow
Solution 5 - Ruby on-Rails-3jobwatView Answer on Stackoverflow