Mock not work after await

2 posts, 0 answers
  1. Rafia
    Rafia avatar
    2 posts
    Member since:
    Apr 2014

    Posted 25 Sep 2014 Link to this post

    try to Mock setUtcOffset method . But after await Request.Content.ReadAsMultipartAsync... Mock doesnot work . Please tell how it'll fix ??

    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();
     public async Task<string> addUpdateMessage()
       if (!Request.Content.IsMimeMultipartContent())
           throw new HttpResponseException(HttpStatusCode.BadRequest);
         var provider = await Request.Content.ReadAsMultipartAsync<InMemoryMultipartFormDataStreamProvider>(new InMemoryMultipartFormDataStreamProvider());
         return "Success";

  2. Stefan
    Stefan avatar
    198 posts

    Posted 29 Sep 2014 Link to this post

    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.


    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.

  3. DevCraft R3 2016 release webinar banner
Back to Top