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

  • Home
  • SEARCH
  • 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 5841935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:57:11+00:00 2026-05-22T11:57:11+00:00

This is a function that I want to write a unit test for: public

  • 0

This is a function that I want to write a unit test for:

public object RequestAndSerializeJSON(string url, Type type) {
    var request = (HttpWebRequest)WebRequest.Create(url);
    request.Accept = "application/json";

    var response = request.GetResponse();
    using (var responseStream = response.GetResponseStream())
    {
        DataContractJsonSerializer serializer = new DataContractJsonSerializer(type);
        return serializer.ReadObject(responseStream);
    }
}

My first thought for testing this was to pass in a path to a file with test data in it for the url, but I can’t do that because I’m casting it to an HttpWebRequest and it throws an error with a file path because it creates a FileWebRequest for file paths.

What is a good way to do this? I’m using the Visual Studio Test Tools for my testing.

  • 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-05-22T11:57:12+00:00Added an answer on May 22, 2026 at 11:57 am

    You have a problem here. As you can see, your code does two things. It gets the request and then it creates an object.

    If you want to unit-test it, i suggest splitting the functionality (which is a nice thing to do anyways)

    (rule of thumb: if the name of your function is very long, uses the ‘and’ word or describes multiple things then you should be refactoring it. we want small methods with small responsabilities, so we can replace them or extend them or do something with them more easily in the future).

    Something like..

    public WebRequest GetRequest(string url);
    public object DeSerializeJSON(WebRequest request, Type type);
    

    might work.

    Now you can inject a mocked request in a test method and test the functionality accordingly.

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

Sidebar

Related Questions

I need to write a unit test for a function that returns a dictionary.
I want to write a unit test which tests the function of a class
I want to write a generic function that has a constraint on the type.
If I have a function that returns an object, but this return value is
I have a function that I use to add vectors, like this: public static
I'm trying to write a unit test with phpunit for a model that uses
I wrote this function that's supposed to do StringPadRight("Hello", 10, "0") -> "Hello00000" .
So I have a function that looks something like this: float function(){ float x
Note that this function does not have a { and } body. Just a
I have a function that looks like this class NSNode { function insertAfter(NSNode $node)

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.