Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create HttpHelper.Fakes nuget package #6

Open
prabirshrestha opened this issue Apr 27, 2012 · 0 comments
Open

create HttpHelper.Fakes nuget package #6

prabirshrestha opened this issue Apr 27, 2012 · 0 comments
Assignees

Comments

@prabirshrestha
Copy link
Member

Create a single HttpHelperFakes.cs file which can be easily added to the test projects using nuget.

This should be independent of Unit Test/Mocking frameworks. In fact mocking should not be used at all since most of these frameworks does not work in WP7/WinRT.

Here some sample usage for Facebook C# SDK.

[Fact]
public void Test()
{
    var fb = new FacebookClient();
    FakeHttpWebRequestWrapper fakeRequest = null;
    FakeHttpWebResponseWrapper fakeResponse = null;

    fb.HttpWebRequestFactory =
        uri => fakeRequest =
               new FakeHttpWebRequestWrapper()
                   .WithRequestUri(uri)
                   .FakeResponse()
                   .WithResponseStreamAs(
                       "{\"id\":\"4\",\"name\":\"Mark Zuckerberg\",\"first_name\":\"Mark\",\"last_name\":\"Zuckerberg\",\"link\":\"http:\\/\\/www.facebook.com\\/zuck\",\"username\":\"zuck\",\"gender\":\"male\",\"locale\":\"en_US\"}")
                   .WithContentType("text/javascript; charset=UTF-8")
                   .WithStatusCode(200)
                   .GetFakeHttpWebRequestWrapper();

    dynamic result = fb.Get("4");

    Assert.Equal("GET", fakeRequest.Method);
    Assert.Equal("https://graph.facebook.com/4", fakeRequest.RequestUri.AbsoluteUri);
    Assert.True(fakeRequest.ContentLength == 0);

    Assert.IsAssignableFrom<IDictionary<string, object>>(result);
    Assert.Equal("4", result.id);
    Assert.Equal("Mark Zuckerberg", result.name);
}

This will allow us to fake the entire HttpWebRequest/HttpWebResponse and we will not need to have the actual request. So integration test would not be required. Since it also executes entire code line by line, test coverage is also highly increased.

It also allows us to test the request properties such as checking if the RequestUri was generated correctly or if the HttpMethod was set correctly and so on.

Also add helper methods for Exceptions and no internet connection.

new FakeHttpWebRequest().NoInternetConnection();

Assumed request got executed but when executing response the internet got disconnected.

 new FakeHttpWebRequestWrapper()
   .WithRequestUri(uri)
   .FakeResponse()
   .NoInternetConnection()
@ghost ghost assigned prabirshrestha Apr 27, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant