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 have a client class which wraps a web service proxy class (as generated by VS IDE) and as such would like to mock the proxy when unit testing my client class.

e.g.

namespace Rbsfm.VTradeWeb.Presentation.Core.StateProviders
{
   /// <summary>
   /// Summary description for SentinelStateProvider.
   /// </summary>
   public class SentinelStateProvider : IStateProvider
   {
      private SessionPersist.SessionPersist sessionStore;

      public SentinelStateProvider()
      {
         this.sessionStore = new SessionPersist.SessionPersist();
      }

      public SentinelStateProvider(SessionPersist.SessionPersist sessionPersist)
      {
         this.sessionStore = sessionPersist;
      }

      #region IStateProvider Members

      public void SetSessionData(string sessionId, string name, object value)
      {
         this.sessionStore.SetSessionData(sessionId, name, value);
      }

      public object GetSessionData(string sessionId, string name)
      {
         return this.sessionStore.GetSessionData(sessionId, name);
      }

      public void ReleaseSession(string sessionId)
      {
         this.sessionStore.ReleaseSession(sessionId);
      }

      #endregion
   }
}


I have tried instance mocking the sessionStore (SessionPersist.SessionPersist) prior to instantiating my SentinelStateProvider class.

I have tried dynamic mocking the SessionPersist.SessionPersist and injecting the dependency both with and without virtualising the methods in the web service proxy. (Virtualising the methods and dynamic mocking with NMock 1.1 works just fine).

I noticed that Tracer reveals that unmocked instances of SessionPersist.SessionPersist are created so I have tried MockAll.

All the above methods have failed.

Do you have a method by which to mock the reference.cs proxy class generated by VS IDE? (before I go insane!)
asked by c0d3-m0nk3y (8.7k points)

6 Answers

0 votes
Hi,
Here is an example of mocking Google API's. The Web Service Proxy was created by VS.NET. The code will not call the web service or even make a network call.

    [TestFixture]
    class TestWebService
    {
        [Test]
        public void search()
        {
            MockManager.Init();
            // mock the service
            Mock searchMock =  MockManager.Mock(
               typeof(WebReference.GoogleSearchService));
            searchMock.ExpectConstructor();
            searchMock.AlwaysReturn("doSpellingSuggestion", "typemock");
            // call the service
            WebReference.GoogleSearchService service = new 
               WebReference.GoogleSearchService();
            Assert.AreEqual("typemock", 
              service.doSpellingSuggestion(_GoogleKey,"helllo"));
        }
    }


In your case you can either mock SentinelStateProvider
MockManager.Init();
// mock the service
Mock proxyMock =  MockManager.Mock(typeof(SentinelStateProvider ));
proxyMock.ExpectConstructor();
proxyMock.AlwaysReturn("GetSessionData", "typemock");


or mock the SessionPersist
Mock sessionMock =  MockManager.Mock(typeof(SessionPersist ));
sessionMock.ExpectConstructor();
sessionMock.AlwaysReturn("GetSessionData", "typemock");


I hope this helps. Please send the test code for further investigation
answered by scott (32k points)
0 votes
I want to mock the SessionPersist web service proxy class because it is the dependency of the SentinelStateProvider class that I am unit testing.

Proxy Class Method Signature:
public object GetSessionData(string sessionId, string name){}


I think my problem arose because I am misunderstanding the params arg for the ExpectAndReturn method.

Succeeds:
this.mockSessionPersist.ExpectAndReturn("GetSessionData", this.expectedValue, 1);


Fails:
this.mockSessionPersist.ExpectAndReturn("GetSessionData", this.expectedValue, 1, new Type[]{typeof(string),typeof(string)});


Can you explain when the params arg should be used - I have had trouble finding a reference to it in the supplied guide .chm (search and index do not appear to be working)
answered by c0d3-m0nk3y (8.7k points)
0 votes
Hi,
The Args is used to validate that the arguments passed are correct.
TypeMock.NET checks that the arguments passed are equal to the ones set in the expection (See Checking Arguments and Built in Checks in the Working With TypeMock.NET chapter of the User Guide)

In your case TypeMock expects a Type object to be passed.
If you ment that the argument must be of certian type, then use:
Check.IsTypeOf()
answered by scott (32k points)
0 votes
Unit Test Fixture:
using System;
using NUnit.Framework;
using Rbsfm.Common.Utilities.Serialization;
using TypeMock;

namespace Rbsfm.VTradeWeb.Presentation.Core.StateProviders
{
   [TestFixture]
   public class SentinelStateProviderTest : IStateProviderTest
   {
      protected SentinelStateProvider sentinelStateProvider;
         
      protected Mock mockSessionPersist;
      protected Mock mockSerializationHelper;

      protected string expectedSessionId;
      protected string expectedName;
      protected string expectedValue;
      protected string expectedSerializedValue;

      [SetUp]
      public void SetUp()
      {
         this.expectedSessionId = Guid.NewGuid().ToString();
         this.expectedName = "TestName";
         this.expectedValue = "TestValue";
         this.expectedSerializedValue = "<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<SOAP-ENC:string id="ref-1">TestValue</SOAP-ENC:string>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
";

         MockManager.Init();

         this.mockSessionPersist = MockManager.Mock(typeof(SessionPersist.SessionPersist), Constructor.NotMocked);
         this.mockSessionPersist.ExpectConstructor();

         this.mockSerializationHelper = MockManager.Mock(typeof(SerializationHelper), Constructor.NotMocked);
         this.mockSerializationHelper.ExpectConstructor();

         this.sentinelStateProvider = new SentinelStateProvider();
      }

      [TearDown]
      public void TearDown()
      {
         MockManager.Verify();
      }

      #region IStateProviderTest Members

      [Test]
      public void SetSessionDataTest()
      {
         this.mockSerializationHelper.ExpectAndReturn("Serialize", this.expectedSerializedValue, 1).Args(this.expectedValue);

         this.mockSessionPersist.ExpectCall("SetSessionData", 1).Args(this.expectedSessionId, this.expectedName, this.expectedSerializedValue);

         this.sentinelStateProvider.SetSessionData(this.expectedSessionId, this.expectedName, this.expectedValue);
      }

      [Test]
      public void GetSessionDataTest()
      {
         this.mockSessionPersist.ExpectAndReturn("GetSessionData", this.expectedSerializedValue, 1).Args(this.expectedSessionId, this.expectedName);

         this.mockSerializationHelper.ExpectAndReturn("Deserialize", this.expectedValue, 1).Args(this.expectedSerializedValue);

         string actual = (string) this.sentinelStateProvider.GetSessionData(this.expectedSessionId, this.expectedName);

         Assert.AreEqual(this.expectedValue, actual);
      }

      [Test]
      public void ReleaseSessionTest()
      {
         this.mockSessionPersist.ExpectCall("ReleaseSession").Args(this.expectedSessionId);

         this.sentinelStateProvider.ReleaseSession(this.expectedSessionId);
      }

      #endregion
   }
}


Class under Test:
using Rbsfm.Common.Utilities.Serialization;

namespace Rbsfm.VTradeWeb.Presentation.Core.StateProviders
{
   /// <summary>
   /// Summary description for SentinelStateProvider.
   /// </summary>
   public class SentinelStateProvider : IStateProvider
   {
      private SessionPersist.SessionPersist sessionStore;
      
      private SerializationHelper serializationHelper;

      public SentinelStateProvider()
      {
         this.sessionStore = new SessionPersist.SessionPersist();
      
         this.serializationHelper = new SerializationHelper();
      }

      #region IStateProvider Members

      public void SetSessionData(string sessionId, string name, object value)
      {
         string xml = this.serializationHelper.Serialize(value);

         this.sessionStore.SetSessionData(sessionId, name, xml);
      }

      public object GetSessionData(string sessionId, string name)
      {
         string xml = (string) this.sessionStore.GetSessionData(sessionId, name);

         return this.serializationHelper.Deserialize(xml);
      }

      public void ReleaseSession(string sessionId)
      {
         this.sessionStore.ReleaseSession(sessionId);
      }

      #endregion
   }
}


SessionPersist web service proxy class mocks just fine but the SerializationHelper mocking is behaving oddly.

SerializationHelper
using System.IO;
using System.Runtime.Serialization.Formatters.Soap;

namespace Rbsfm.Common.Utilities.Serialization
{
   public class SerializationHelper
   {
      public SerializationHelper()
      {
         
      }

      public string Serialize(object sessionObject)
      {
         MemoryStream memoryStream = new MemoryStream();

         SoapFormatter soapFormatter = new SoapFormatter();

         soapFormatter.Serialize(memoryStream, sessionObjec
answered by c0d3-m0nk3y (8.7k points)
0 votes
Hi,
Please run the TypeMock Tracer Utility.
Here you will see what instance of SerializationHelper is being used.

In any event try MockManager.Init() in SerializationHelperTest (although this test doens't require mocking, this will start insterting mock code in your classes (TypeMock cannot mock classes that have been called already before Init was called). This will make SerializationHelper mackable.

In future versions we are going to solve this scenario.
answered by scott (32k points)
0 votes
This is solved with version 3.1 where automatic initilization is implemented for known testing frameworks
answered by scott (32k points)
...