Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define an array / hash in factory_bot?

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)
like image 927
Doug Avatar asked Apr 05 '12 16:04

Doug


3 Answers

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)
like image 60
doublemarked Avatar answered Nov 04 '22 10:11

doublemarked


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" }
like image 26
jethroo Avatar answered Nov 04 '22 09:11

jethroo


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.

like image 8
milgner Avatar answered Nov 04 '22 09:11

milgner