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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:46:24+00:00 2026-06-18T02:46:24+00:00

I am building an API wrapper and am writing some tests for it and

  • 0

I am building an API wrapper and am writing some tests for it and I have a couple of questions.

1) How do I write an assert for calls where data doesn’t exist? For example, looking up a member by id using the API but the user won’t exist yet.
2) How do I write an assert for testing PUT and DELETE requests?

I already have a grasp on testing GET and POST requests just not sure on the other 2 verbs.

  • 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-18T02:46:25+00:00Added an answer on June 18, 2026 at 2:46 am

    For your question part 1…

    You have a couple choices for data that doesn’t exist:

    1. You can create the data ahead of time, for example by using a test seed file, or a fixture, or a factory. I like this choice for larger projects with more sophisticated data arrangements. I also like this choice for getting things working first because it’s more straightfoward to see the data.

    2. You can create a test double, such as a stub method or fake object. I like this choice for fastest test performance and best isolation. For fastest tests, I intercept calls as early as possible. The tradeoff is that I’m not doing end-to-end testing.

    For your question part 2…

    You should edit your question to show your actual code; this will help people here answer you.

    Is your VCR code is something like this?

    VCR.use_cassette('test_unit_example') do
      response = Net::HTTP.get_response('localhost', '/', 7777)
      assert_equal "Hello", response.body
    end
    

    If so, you change the HTTP get to put, something like this:

    uri = URI.parse(...whatever you want...)
    json = "...whatever you want..."
    req = Net::HTTP::Put.new(uri)
    req["content-type"] = "application/json"
    req.body = json
    request(req)
    

    Same for HTTP delete:

    Net::HTTP::Delete.new(uri)
    

    A good blog post is the http://www.rubyinside.com/nethttp-cheat-sheet-2940.html>Net::HTTP cheat sheet excerpted here:

    # Basic REST.
    # Most REST APIs will set semantic values in response.body and response.code.
    require "net/http"
    
    http = Net::HTTP.new("api.restsite.com")
    
    request = Net::HTTP::Post.new("/users")
    request.set_form_data({"users[login]" => "quentin"})
    response = http.request(request)
    # Use nokogiri, hpricot, etc to parse response.body.
    
    request = Net::HTTP::Get.new("/users/1")
    response = http.request(request)
    # As with POST, the data is in response.body.
    
    request = Net::HTTP::Put.new("/users/1")
    request.set_form_data({"users[login]" => "changed"})
    response = http.request(request)
    
    request = Net::HTTP::Delete.new("/users/1")
    response = http.request(request)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a Java wrapper for an API and I have a fundamental question
I am building a Web API using MVC4 and some request return blocks of
I'm building an application that uses an elaborate API to fetch data from Youtube,
I'm currently building a library around the Google Analytics Data Export API. When i'm
I'm building an API API, it's basically a wrapper for a in house REST
I am building some update statements with the MongoDB C# driver. The C# API
I'm building a class wrapper for the mysql c api, specifically at the moment
I'm building a wrapper for the HTML5 File(Reader) API with a fallback to normal
I am building a app with many different parts which access remote api calls
I have been building an API which returns XML code. For example, the API

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.