chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
I'm trying to make a mocked object ( HttpWebRequest.GetResponse() ) return another mocked object ( HttpWebResponse ). and I'm getting:
TypeMock.TypeMockException: 
*** No method GetResponse in type System.Net.HttpWebRequest returns TypeMock.MockObject
   at TypeMock.Mock.a(String A_0, Object A_1, Boolean A_2, Boolean A_3, Int32 A_4, Type[] A_5)
   at TypeMock.Mock.ExpectAndReturn(String method, Object ret, Int32 timesToRun, Type[] genericTypes)
   at TypeMock.Mock.ExpectAndReturn(String method, Object ret, Type[] genericTypes)
   at SI.Swiftbit.P2p.Tests.TrackerClientTest.TestNormalGetPeers() in c:Documents and SettingsSpookyETMy DocumentsVisual Studio 2005ProjectsSI.SwiftbitSI.Swiftbit.P2p.TestsTrackerClientTest.cs:line 138


For this code:

#region Using Directives

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Web;
using System.Text;
using NUnit.Framework;
using TypeMock;
using SI.Swiftbit.P2p;
using SI.Swiftbit.P2p.Utilities;

#endregion

namespace SI.Swiftbit.P2p.Tests
{
   [TestFixture]
   public class TrackerClientTest
   {
      TrackerClient trackerClient;
      Mock httpWebRequest;
      Mock httpWebResponse;
      byte[] filesKey = Encoding.UTF8.GetBytes("files");
      byte[] completeSourcesKey = Encoding.UTF8.GetBytes("complete");
      byte[] incompleteSourcesKey = Encoding.UTF8.GetBytes("incomplete");
      byte[] downloadedKey = Encoding.UTF8.GetBytes("downloaded");
      byte[] nameKey = Encoding.UTF8.GetBytes("name");
      
      [SetUp]
      public void Initialize()
      {
         MockManager.Init();
         httpWebRequest = MockManager.Mock(typeof(HttpWebRequest));
         httpWebResponse = MockManager.Mock(typeof(HttpWebResponse));
         trackerClient = new TrackerClient(
            new Uri("http://announce.com:6969/announce"));
      }
      
      [TearDown]
      public void Dispose()
      {
         MockManager.Verify();
      }
      
      [Test]
      public void TestNormalGetPeers()
      {
         PeersInfo peersInfo;
         Peer[] peers;
         SortedDictionary<byte[], object> responseData =
            new SortedDictionary<byte[], object>(new ByteArrayComparer());
         
         
         List<SortedDictionary<byte[], object>> peersList =
            new List<SortedDictionary<byte[], object>>();
         
         SortedDictionary<byte[], object> peer1 =
            new SortedDictionary<byte[], object>(new ByteArrayComparer());
         
         SortedDictionary<byte[], object> peer2 =
            new SortedDictionary<byte[], object>(new ByteArrayComparer());
         
         byte[] peerId1 = new byte[20]
         {
            1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
         };
         
         byte[] peerId2 = new byte[20]
         {
            4, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
         };
         
         byte[] infoKeyHash = new byte[20]
         {
            7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
         };
         
         string warningMessage = "This is a tracker warning.";
         
         peer1.Add(Encoding.UTF8.GetBytes("peer id"), peerId1);
         peer1.Add(
            Encoding.UTF8.GetBytes("ip"),
            Encoding.UTF8.GetBytes("1.1.1.1"));
         
         peer1.Add(Encoding.UTF8.GetBytes("port"), 6969);
         
         peer2.Add(Encoding.UTF8.GetBytes("peer id"), peerId2);
         peer2.Add(
            Encoding.UTF8.GetBytes("ip"),
            Encoding.UTF8.GetBytes("2.2.2.2"));
         
         peer2.Add(Encoding.UTF8.GetBytes("port"), 7000);
         
         responseData.Add(Encoding.UTF8.GetBytes("interval"), 5);
         responseData.Add(Encoding.UTF8.GetBytes("min interval"), 2);
         responseData.Add(Encoding.UTF8.GetBytes("complete"), 10);
         responseData.Add(Encoding.UTF8.GetBytes("incomplete"), 20);
         responseData.Add(
            Encoding.UTF8.GetBytes("warning message"),
            warningMessage);
         
         responseData.Add(Encoding.UTF8.GetBytes("peers"), peersList);
         
         httpWebRequest.ExpectAndReturn(
            "GetResponse",
            httpWebResponse,
            null);
         
asked by Dog_Soldier (3.1k points)

15 Answers

0 votes
In that ReadResponse method above, I don't actually need WebRequest and WebResponse to be casted to HttpWebRequest and HttpWebResponse. So deleteh "Http". The mocks still don't get called.
answered by Dog_Soldier (3.1k points)
0 votes
Hi,

The problem here is that we cannot create the HttpWebResponse as MockObject only calls public constructors.
:idea: We will add a feature to enable calling non-public constructors

In the mean time here is a work around:

    // mock next WebRequest
    Mock httpWebRequestMock = MockManager.Mock(typeof(HttpWebRequest));

    // mock next WebResponse
    Mock httpWebResponseMockControl = MockManager.Mock(typeof(HttpWebResponse));
    // args for WebResponse
    object[] args = new object[] { new SerializationInfo(typeof(string),
      new FormatterConverter()),
      new StreamingContext(StreamingContextStates.All)};

    // create the WebResponse
    HttpWebResponse mockHttpWebResponse = 
       Activator.CreateInstance(typeof(HttpWebResponse),
       BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, 
       null,args,null) as HttpWebResponse;

    // set Request to return our mockResponse         
    httpWebRequestMock.ExpectAndReturn("GetResponse",
       mockHttpWebResponse);


Hope this helps
answered by scott (32k points)
0 votes
Hello,
I have the same problem as Dog_Soldier and i tried to work around Scott but it didnt help. Im not sure if the work around fixed the Dog_Soldier's problem.

The error message i get is:
"Constructor on type 'System.Net.HttpWebResponse' not found."

And I'm using VS 2005 and ASP.NET 2.0
answered by Rajee (180 points)
0 votes
Hello,
I have the same problem as Dog_Soldier and i tried to work around Scott but it didnt help. Im not sure if the work around fixed the Dog_Soldier's problem.

The error message i get is:
"Constructor on type 'System.Net.HttpWebResponse' not found."

And I'm using VS 2005 and ASP.NET 2.0


I'm working with VB.NET so I had some problem with converting work around code from C# to VB,so the solution seems to work having problem with TypeMock license.

Thanks
answered by Rajee (180 points)
0 votes
Hi Rajee
Glad you work it out :D
What is the problem with TypeMock license?
answered by ohad (35.4k points)
...