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
Hi,
I am trying to mock a method call which is inside a "using" statement. The method I wanted to mock is inside the class which I have used in using statement. The class used in using statement is a derived class. It seems I need to mock the Close() and/or Dispose method of base object. But I don't know how. Could you please suggest me something?

The Test script:

[Test, VerifyMocks]
public void SubmittedFormWithValidUserInput()
{
var dummy = Model.Client;
var fakeList = secQuestions;
var fakeErrorList = new Fault[]{};
using (var recorder = RecorderManager.StartRecording())
{
recorder.ExpectAndReturn(dummy.RegisterCustomerProfile(null), fakeErrorList).RepeatAlways();
}
setupListObject(fakeList);
m_presenter.view_Submit(this, EventArgs.Empty);

}

Method to test:

public void view_Submit(object sender, EventArgs e)
{
CustomerProfileRegistrationRequest custProfile = new CustomerProfileRegistrationRequest
{
Customer = m_view.customerBinder,
CustomerConfidential = m_view.ConfidentialBinder,
CustomerSurvey = m_view.SurveyBinder,
CustomerSecurity = m_view.SecurityBinder,
Address = m_view.addressBinder
};
//TODO using

using (var client = Model.Client)
{

if (client.RegisterCustomerProfile(custProfile).Length == 0)
{
m_view.Close();
}
else
{
m_view.ForwardTo<IUserIdCreateView>();
}
}
}

Error :

UserIdCreatePresenterTests.SubmittedFormWithValidUserInput : FailedSystem.NullReferenceException: Object reference not set to an instance of an object.
at System.ServiceModel.ClientBase`1.GetChannelFactory()
at System.ServiceModel.ClientBase`1.Close()
at System.ServiceModel.ClientBase`1.System.IDisposable.Dispose()
at InfoSpherix.HuFi.UI.Public.Presenters.UserIdCreatePresenter.view_Submit(Object sender, EventArgs e) in UserIdCreatePresenter.cs: line 115
at InfoSpherix.HuFi.UI.Public.UnitTests.UserIdCreatePresenterTests.SubmittedFormWithValidUserInput() in UserIdCreatePresenterTests.cs: line 142
at TypeMock.VerifyMocksAttribute.Execute()
at TypeMock.DecoratorAttribute.CallDecoratedMethod()
at TypeMock.ClearMocksAttribute.Execute()
at TypeMock.MethodDecorator.e()
at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Object[] A_5)
at TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected)
at InfoSpherix.HuFi.UI.Public.UnitTests.UserIdCreatePresenterTests.SubmittedFormWithValidUserInput() in UserIdCreatePresenterTests.cs: line 133


Thanks,
Mukesh[/b]
asked by mukesh (640 points)

2 Answers

0 votes
Hi Mukesh,
The issue is that the using statement always makes sure that Dispose is called on the used object. and the compiler automatically changs the code to the following:
try
{
  var client = Model.Client;
  ...
} finally {
  client.Dispose();
}

:idea: So as you see the Dispose is called, but we didn't mock it.
Although I haven't coded this, I assume that the following will solve your problem:
using (var expect = RecorderManager.StartRecording())
{
   // we are mocking the using code too
   using (var mockedClient = Model.Client)
   {
       expect.ExpectAndReturn(mockedClient.RegisterCustomerProfile(null), fakeErrorList).RepeatAlways();
   }
}
answered by eli (5.7k points)
0 votes
Thanks. Now its working.
answered by mukesh (640 points)
...