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 8998239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:59:50+00:00 2026-06-15T23:59:50+00:00

Using RSpec, is there a way to compare two hashes where I don’t know

  • 0

Using RSpec, is there a way to compare two hashes where I don’t know what some values will be? I’ve tried using instance_of, but it doesn’t seem to work.

In my test, I am creating a new device object with a POST request and want to make sure I get the right JSON response back. It’s creating a UUID for the device (I’m not using a relational db), but I obviously won’t know what that value will be and don’t really care.

post "/projects/1/devices.json", name: 'New Device'

expected = {'name' => 'New Device', 'uuid' => instance_of(String), 'type' => 'Device'}

JSON.parse(body).should == expected

And I get the following error:

 1) DevicesController API adds a new device to a project
     Failure/Error: JSON.parse(body).should == expected
       expected: {"name"=>"New Device", "uuid"=>#<RSpec::Mocks::ArgumentMatchers::InstanceOf:0x5a27147d @klass=String>, "type"=>"Device"}
            got: {"name"=>"New Device", "uuid"=>"ef773465-7cec-48fd-b2a7-f1da10d1595a", "type"=>"Device"} (using ==)
       Diff:
       @@ -1,4 +1,4 @@
        "name" => "New Device",
        "type" => "Device",
       -"uuid" => #<RSpec::Mocks::ArgumentMatchers::InstanceOf:0x5a27147d @klass=String>
       +"uuid" => "ef773465-7cec-48fd-b2a7-f1da10d1595a"
     # ./spec/api/devices_spec.rb:37:in `(root)'
  • 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-15T23:59:51+00:00Added an answer on June 15, 2026 at 11:59 pm

    instance_of is for argument matching:

    something.should_receive(:foo).with(instance_of(String))
    

    Instead of using the == operator you could use include. For example:

    JSON.parse(body).should include('uuid', 'name' => 'New Device', 'type' => 'Device')
    

    which says that the key uuid must be present with any value and name and type should be present with the specified value. It will also allow other keys in the hash.

    I don’t know of a way to achieve exactly what you’re asking for with the built-in matchers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to do integration tests with Rspec without using Cucumber? I
Does anyone know how to test page_caching in Rails using RSpec without having to
Are there strong reasons for using Rspec for controllers and views too? My views
I'm still on Rails 2.3.x and using Rspec, Capybara and Steak to do some
I am using some spec_helper.rb methods to generate RSpec examples and one of them
I'm writing some automated tests using Capybara/RSpec, I choose/configure the driver by using this
I have just started using RSpec and I copied the very simple test on
I recently started using rspec and factory_girl and I'm working on a basic control
I have recently started using RSpec for Integration testing in my Rails application, to
I am using rspec and cucumber to run watir tests at sauce labs. I

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.