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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:34:19+00:00 2026-06-14T19:34:19+00:00

I’m writing test using double/stub, and i want to return me a fake value,

  • 0

I’m writing test using double/stub, and i want to return me a fake value, but instead i’m getting and fake value and real value. Here is my controller and spec files, model has nothing in it. Any ideas what am i doing wrong? My guess is i’m doing it completely wrong. P.s. i’m kinda new to spec and mock/stub things

controller:

def destroy
    uri = URI('example.com')
    request_params = {'requestId' => @session.id, 
      'merchantId' => MERCHANTID, 
      'locale' => LOCALE, 
      'partnerId' => PARTNERID, 
      'sessionId' => params[:id]
    }

   Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
      request = Net::HTTP::Post.new uri.request_uri
      request.set_form_data(request_params)
      #request.content_type = 'application/x-www-form-urlencoded'
      response = http.request request # Net::HTTPResponse object
      json = ActiveSupport::JSON.decode(response.body)
      puts '<<<<<<<<<'
      puts json
      if json['expireSessionRes']['isError'] == false
        render :json => {:status => 'ok', :message => 'logged out'}
      else
        render :json => {:status => 'failed', :message => 'logout failed'}
      end
    end

spec file:

it "should correctly return isError false if logout was sucessful" do
    SID = "123"
    @data3 = {:id => SID}  
    delete :destroy, default_rest_params.merge(@data3)
    response = double('http.request')
    response.stub!(:body => '{"expireSessionRes"=>{"requestId"=>"72", "sessionId"=>"123", "isError"=>false, "desc"=>""}}')
    puts response.body


  end

receiving:

{"expireSessionRes"=>{"requestId"=>"94", "isError"=>true, "desc"=>"Session exipred", "errorCode"=>"-10011"}}
{"expireSessionRes"=>{"requestId"=>"72", "sessionId"=>"123", "isError"=>false, "desc"=>""}}

first comes from controller
second from stub

  • 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-14T19:34:20+00:00Added an answer on June 14, 2026 at 7:34 pm

    So the thing with mocking and stubbing is, you have to hook your mock objects up to your real code somehow… the double('http.request') bit just creates a mock object called “http.request”, it doesn’t make http.request return this object.

    What you need to do is stub the request method to return the mock response. But it’s going to be pretty difficult to inject a mock, because you don’t have access to the http object in the block from your tests.

    Difficulty testing code is often a good sign that you need to refactor your code.

    The simplest thing would be to replace the whole Net::HTTP.start thing with a single line

    response = Net::HTTP.post_form(uri, request_params)
    

    and then you can test it by doing

    remote_response = double('remote_response', body: '...')
    Net::HTTP.stub(:post_form).and_return(remote_response)
    
    delete :destroy, ...
    
    response.should whatever
    

    (I assume you’re in a controller spec and you’re going to need the response object that RSpec provides, so your mock can’t be stored in a variable called response).

    But that is just glossing over the more important point that you should be extracting your remote service interaction out into a dedicated class and testing that separately, it doesn’t belong in the controller.

    Hope that helps!

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

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I want to construct a data frame in an Rcpp function, but when I
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
Seemingly simple, but I cannot find anything relevant on the web. What is the
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.