Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Facebook C# SDK supports your scenario for mocking the entire HttpWebRequest and HttpWebResponse. In fact we actually use that internally in our unit tests so that every single line of the code in Facebook C# SDK actually gets executed and the result is always the same. <a href="https://github.com/facebook-csharp-sdk/facebook-csharp-sdk/blob/v5/Source/Facebook.Tests/TestExtensions.cs" rel="nofollow">https://github.com/facebook-csharp-sdk/facebook-csharp-sdk/blob/v5/Source/Facebook.Tests/TestExtensions.cs</a> For now you will need to check these tests in v5 branch as we haven't yet migrated those tests to v6.</p> <p>For v5, you will need to override the protected <code>CreateHttpWebRequest</code> method in FacebookClient.</p> <p>Here is an example for v5 when there is no internet connection. There are three hidden classes HttpWebRequestWrapper, HttpWebResponseWrapper and WebExceptionWrapper that you will need to make use of.</p> <pre><code> public static void NoInternetConnection(this Mock&lt;Facebook.FacebookClient&gt; facebookClient, out Mock&lt;HttpWebRequestWrapper&gt; mockRequest, out Mock&lt;WebExceptionWrapper&gt; mockWebException) { mockRequest = new Mock&lt;HttpWebRequestWrapper&gt;(); mockWebException = new Mock&lt;WebExceptionWrapper&gt;(); var mockAsyncResult = new Mock&lt;IAsyncResult&gt;(); var request = mockRequest.Object; var webException = mockWebException.Object; var asyncResult = mockAsyncResult.Object; mockRequest.SetupProperty(r =&gt; r.Method); mockRequest.SetupProperty(r =&gt; r.ContentType); mockRequest.SetupProperty(r =&gt; r.ContentLength); mockAsyncResult .Setup(ar =&gt; ar.AsyncWaitHandle) .Returns((ManualResetEvent)null); mockWebException .Setup(e =&gt; e.GetResponse()) .Returns&lt;HttpWebResponseWrapper&gt;(null); mockRequest .Setup(r =&gt; r.GetResponse()) .Throws(webException); mockRequest .Setup(r =&gt; r.EndGetResponse(It.IsAny&lt;IAsyncResult&gt;())) .Throws(webException); AsyncCallback callback = null; mockRequest .Setup(r =&gt; r.BeginGetResponse(It.IsAny&lt;AsyncCallback&gt;(), It.IsAny&lt;object&gt;())) .Callback&lt;AsyncCallback, object&gt;((c, s) =&gt; { callback = c; }) .Returns(() =&gt; { callback(asyncResult); return asyncResult; }); var mockRequestCopy = mockRequest; var mockWebExceptionCopy = mockWebException; facebookClient.Protected() .Setup&lt;HttpWebRequestWrapper&gt;("CreateHttpWebRequest", ItExpr.IsAny&lt;Uri&gt;()) .Callback&lt;Uri&gt;(uri =&gt; { mockRequestCopy.Setup(r =&gt; r.RequestUri).Returns(uri); mockWebExceptionCopy.Setup(e =&gt; e.Message).Returns(string.Format("The remote name could not be resolved: '{0}'", uri.Host)); }) .Returns(request); } </code></pre> <p>You can then write your tests as below.</p> <pre><code> [Fact] public void SyncWhenThereIsNotInternetConnectionAndFiddlerIsNotOpen_ThrowsWebExceptionWrapper() { var mockFb = new Mock&lt;FacebookClient&gt; { CallBase = true }; Mock&lt;HttpWebRequestWrapper&gt; mockRequest; Mock&lt;WebExceptionWrapper&gt; mockWebException; mockFb.NoInternetConnection(out mockRequest, out mockWebException); Exception exception = null; try { var fb = mockFb.Object; fb.Get(_parameters); } catch (Exception ex) { exception = ex; } mockFb.VerifyCreateHttpWebRequest(Times.Once()); mockRequest.VerifyGetResponse(); mockWebException.VerifyGetReponse(); Assert.IsAssignableFrom&lt;WebExceptionWrapper&gt;(exception); } </code></pre> <p>In v6 we have made mocking the HttpWebRequest and HttpWebResponse much easier.</p> <p>Create your custom HttpWebRequest and HttpWebResponse by inheriting <code>HttpWebRequestWrapper</code> and <code>HttpWebReponseWrapper</code>.</p> <p>Then change the default http web request factory for Facebook C# SDK. Here is the sample of the default factory.</p> <pre><code>FacebookClient.SetDefaultHttpWebRequestFactory(uri =&gt; new HttpWebRequestWrapper((HttpWebRequest)WebRequest.Create(uri))); </code></pre> <p>If you want to change the HttpWebRequestFactor per FacebookClient instance then use the following code.</p> <pre><code>var fb = new FacebookClient(); fb.HttpWebRequestFactory = uri=&gt; new MyHttpWebRequestWrapper(uri); </code></pre> <p>Note: HttpWebRequestWrapper, HttpWebResponseWrapper, WebExceptionWrapper, FacebookClient.SetDefaultHttpWebRequestFactory and FacebookClient.HttpWebRequestFactory has the attribute <code>[EditorBrowsable(EditorBrowsableState.Never)]</code> so you might not see it in the intellisense.</p> <p>Things like no internet connection that you mention should actually be a part of facebook c# sdk tests and not your app unit tests. The sdk should guarantee that when there is not internet conenction it always throws WebExceptionWrapper and your app unit tests should actually be handling the WebExceptionWrapper exception and not mocking the entire httpwebrequest and httpwebresponse.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload