Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8400515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:34:38+00:00 2026-06-09T21:34:38+00:00

I am trying to write a test that simulates some return values from Dropbox’s

  • 0

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:34:39+00:00Added an answer on June 9, 2026 at 9:34 pm

    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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a unit test that checks some xml parsing code. The
I'm trying to write a test that invokes a web service and tests it's
I am trying to write some front end Javascript tests that can test drag
I'm trying to write a cucumber test that ensures there are no errors from
I'm trying to write a unit test that will loop through all action methods
I'm trying to write a unit test that will raise an event on a
I'm trying to write a unit test for a piece of code that generates
I am trying to write a Unit test for a class that has several
I'm trying to write basic assert test: def assert_session_has ( sessionvar ) return assert_not_nil
I'm trying to write a test that returns a data-reader with one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.