How to define an array / hash in factory_bot?
Asked Answered
S

5

82

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)
Swinge answered 5/4, 2012 at 16:54 Comment(3)
Do you really need a factory for this? Why not just define a method that returns the simulated response?Meill
That's what I ended up doing. But I thought the point of Factories was to isolate this stuff. I'm still curious - seems like Hash and Array are classes and this should work if I can just get the right syntax.Swinge
I've only used them for generating ActiveRecord model instances. FactoryGirl is intended to replace fixtures. You might take a look at RSpec's helper methods: relishapp.com/rspec/rspec-core/v/2-9/docs/helper-methodsMeill
P
166

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)
Pope answered 19/8, 2012 at 13:45 Comment(7)
This is a great solution. It even works with factories' inheritance.Fourchette
This works for me to creat/build, but it fails the lint: FacoryGirl.lintSwastika
Thank you for this. To pass the Rubocop linter in the favorites multi-line block use do..end instead of {{..}}.Apiculate
you can add skip_create to the factory if you want to call 'create' instead of buildRetroact
Great. I found I can initialise any custom class, initialize_with evaluates in class context, i.e. if I have initialize_with { new(attributes) }, build :my_factory will return result of MyClass.new(attributes)Hathor
Thank you. This is great for stubbing out the array of hashes returned by a database query.Bosk
@Hathor if you are initializing as a class instance, you may be better off passing it to your factory definition. e.g.: factory :my_factory, class: MyClass do ... endEmbed
M
11

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" }
Motheaten answered 29/10, 2014 at 12:5 Comment(2)
This will also has the added benefit of allowing the keys to be strings.Bilinear
the downside here is you're modifying a single object and not a new one each time it is created. Take a look at the object_id of the hash'sBillman
F
8

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.

Fleshy answered 17/7, 2014 at 12:48 Comment(2)
The gem was renamed to FactoryBot. The new location for that documentation is rubydoc.info/github/thoughtbot/factory_bot/FactoryBot/Syntax/…Apiculate
This will only work if you have a class to be linked to your factory definition (e.g.: factory :my_class do ... end will try to instantiate MyClass class). To overcome this, you can pass class parameter though. factory :my_class, class Hash do ... endEmbed
E
6

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.
Edwards answered 3/5, 2012 at 12:28 Comment(0)
B
1

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

Bigg answered 7/11, 2012 at 5:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.