This is a migrated thread and some comments may be shown as answers.

Mock not work after await

1 Answer 106 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Rafia
Top achievements
Rank 1
Rafia asked on 25 Sep 2014, 12:32 PM
try to Mock setUtcOffset method . But after await Request.Content.ReadAsMultipartAsync... Mock doesnot work . Please tell how it'll fix ??

[TestMethod]
public async Task TestUploadEntity()
{
    IContainer container = (IContainer)TestContext.Properties["IContainer"];
    BaseApiController apicontroller = container.Resolve<BaseApiController>();
    Mock.NonPublic.Arrange(apicontroller, "setUtcOffset").IgnoreArguments().IgnoreInstance();
      
    var config = new HttpConfiguration();
    var route = config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}");
    var routeData = new HttpRouteData(route, new HttpRouteValueDictionary { { "controller", "Mobile" } });
    MultipartFormDataContent formDataContent = new MultipartFormDataContent();
    formDataContent.Add(new StringContent(Guid.NewGuid().ToString()), name: "EntityID");
    controller.Request = request;
    controller.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config;
    var result = await controller.addUpdateMessage();
}
  
[HttpPost]
 public async Task<string> addUpdateMessage()
 {
   if (!Request.Content.IsMimeMultipartContent())
    {
       throw new HttpResponseException(HttpStatusCode.BadRequest);
    }
     var provider = await Request.Content.ReadAsMultipartAsync<InMemoryMultipartFormDataStreamProvider>(new InMemoryMultipartFormDataStreamProvider());
     setUtcOffset();
     return "Success";
 }

1 Answer, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 29 Sep 2014, 10:28 AM
Hi Rafia,

It appears that you're mixing future mocking (IgnoreInstance) with async tests. A future or static arrangement works across threads only when you add the .OnAllThreads() clause to the arrangement, i.e.:
Mock.NonPublic.Arrange(apicontroller, "setUtcOffset").IgnoreArguments().IgnoreInstance().OnAllThreads();
Future or static mocking across threads is dangerous when you mock framework classes. Doing so could potentially destabilize the testing framework itself. Since you're mocking a non-framework class here, there should be no problem.

Regards,
Stefan
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
General Discussions
Asked by
Rafia
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Share this question
or