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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:14:01+00:00 2026-06-15T16:14:01+00:00

Im trying to test a create method that has a call to an external

  • 0

Im trying to test a create method that has a call to an external API but I’m having trouble mocking the external API request. Heres my setup and what I’ve tried so far:

class Update
  def self.create(properties)
    update = Update.new(properties)

    begin
      my_file = StoreClient::File.get(properties["id"])
      update.filename = my_file.filename
    rescue
      update.filename = ""
    end    

    update.save  
  end
end


context "Store request fails" do
  it "sets a blank filename" do
    store_double = double("StoreClient::File")
    store_double.should_receive(:get).with(an_instance_of(Hash)).and_throw(:sad)
    update = Update.create({ "id" => "222" })
    update.filename.should eq ""        
  end
end

at the moment Im getting this failure

 Failure/Error: store_double.should_receive(:get).with(an_instance_of(Hash)).and_throw(:sad)
   (Double "StoreClient::File").get(#<RSpec::Mocks::ArgumentMatchers::InstanceOf:0x000001037a9208 @klass=Hash>)
       expected: 1 time
       received: 0 times

why is my double not working and how is best to mock the call to StoreClient::File.get, so that I can test the create method when it succeeds or fails?

  • 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-15T16:14:02+00:00Added an answer on June 15, 2026 at 4:14 pm

    The problem is that double("StoreClient::File") creates a double called “StoreClient::File”, it does not actually substitute itself for the real StoreClient::File object.

    In your case I don’t think you actually need a double. You can stub the get method on the StoreClient::File object directly as follows:

    context "Store request fails" do
      it "sets a blank filename" do
        StoreClient::File.should_receive(:get).with(an_instance_of(Hash)).and_throw(:sad)
        update = Update.create({ "id" => "222" })
        update.filename.should eq ""        
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a unit test for a method that takes a
I am trying to call a method that has some xml functionality and It
Specifically, I'm trying to create a unit test for a method which requires uses
I'm trying to create a JQuery function that will test a string for the
I'm trying to create a class that has a map of keys -> function
I am trying to create a Jquery plugin that maintains chainability and has public
I am trying to create a test harness for our QA staff to test
I am trying to learn how to create and test for the expiration of
I have the below configuration where I'm trying to create a test C function
I'm trying to create an Android test project from within my main project. Exactly

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.