How to define an array / hash in factory_bot?

Doug picture Doug · Apr 5, 2012 · Viewed 37k times · Source

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)

Answer

doublemarked picture doublemarked · Aug 19, 2012

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)