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
Scenario: I have a web service client class which is consuming the reference.cs generated by VSNet2003 from the wsdl url.
The web service provides a class type which has a public field of type string - this class type is used as an argument for one of the method calls.
I construct a new instance of this type during my wrapper method, set the string field value and then call the webmethod wrapper in the reference.cs - how can I verify that the string field is set?
e.g.

public string[] GetSupportedIndexes(string currency)
      {
         try
         {
            getSupportedIndexes getSupportedIndexes = new getSupportedIndexes();
            getSupportedIndexes.String_1 = currency;
            return this.tradeServicesClient.getSupportedIndexes(getSupportedIndexes);
         }
         catch (Exception e)
         {
            throw new TradeServicesProviderException("WebMethod getSupportedIndexes threw an exception", e); 
         }
      }

I can ExpectConstructor() but can't think of how to verify the String_1 field gets set - ExpectSet fails since it expects a proeprty setter and String_1 is a field....

[Test]
      public void GetSupportedIndexesTest()
      {
         this.mockGetSupportedIndexes.ExpectConstructor();

         this.mockSWM_TradeService.ExpectAndReturn("getSupportedIndexes", this.expectedIndices).Args(Check.IsMock(this.mockGetSupportedIndexes));

         string[] actual = this.jBossTradeServicesProvider.GetSupportedIndexes(this.expectedCurrency);

         Assert.AreSame(this.expectedIndices, actual);
      }


I *could* ask the web service developers to make 2 methods on the class for setting and getting the field value but that seems a bit lame....

I *could* not mock the getSupportedIndexes class and make it a private field, get and set values to check via reflection but again it seems a bit over the top to test that a single field is getting set....

I tried creating a test webservice with a public class containing a property encapsulating string1 private field but the wsdl tool converted it to a public field....

Any ideas? :?
asked by c0d3-m0nk3y (8.7k points)

2 Answers

0 votes
Hi,
It is true that you cannot mock access to fields yet.

One way to test this is by using your own argument checked.

Here is an example:

public static bool CheckCurrencyIsSet(ParameterCheckerEventArgs args)
{
   getSupportedIndexes indexes = (getSupportedIndexes)args.ArgumentValue;
   return indexes.String_1 == this.expectedCurrency;
}
// in the code...
his.mockSWM_TradeService.ExpectAndReturn("getSupportedIndexes", this.expectedIndices).Args(new ParameterCheckerEx(CheckCurrencyIsSet)); 
answered by scott (32k points)
0 votes
:idea: TypeMock.NET version 3.5.1 now helps you in these scenarios use the MockedInstace property.

You can now test the scenario as follows:

[Test]
public void GetSupportedIndexesTest()
{
    // Setup mocks 
    ...
    // Call the test method
    ...

    // get the object created inside CheckCurrencyIsSet
    getSupportedIndexes actualGetSupportedIndexes = mockGetSupportedIndexes.MockedInstance as getSupportedIndexes;
    Assert.AreEqual(this.expectedCurrency, actualGetSupportedIndexes.String_1 );
} 
answered by scott (32k points)
...