What techniques may I use for mocking interactions with TaskFactory.FromAsync()?

I am attempting to unit test the interactions of my application and the asynchronous HttpWebRequest methods and am wondering how to simulate the call of the asynchronous callback. For instance, consider the following code:

Task.Factory .FromAsync<Stream>(wr.BeginGetRequestStream, wr.EndGetRequestStream, null) .ContinueWith(SomeDelegate);

In this case wr is an object that essentially wraps HttpWebRequest. I can stub and set expectations on this object in the unit test, but I can not think of a way to create the signal that would otherwise call EndRequestStream. Without this signal, the unit test blocks indefinitely waiting for the task to complete, which will never happen.

I can create interfaces and proxies for Task and TaskFactory, then set expectations at that level, but I am hoping there is something less involved.

-------------Problems Reply------------

Can you change the type of wr to expose a GetRequestAsync method that returns a Task<Stream>? That way you can mock it so that it returns a task from a TaskCompletionSource which you can prod within your test. I've done something similar with a class myself to test a composition block, and it worked well. The tests are interesting to write, in terms of all the different things that can happen, but it works...

I'm not sure I got queston right, if you want to simulate Event Raise you can use following syntax:

// wr should be Mock
// EndRequestStream - event
wr.Raise(x => x.EndRequestStream += null,
new EventArgs(...));

Do not forget to put right EventArgs instance.

Category:mocking Views:0 Time:2011-11-16

Related post

  • Whats the best way to create interactive application prototypes? 2008-09-26

    The question should be interpreted from a general point of view and not targeted solely at web apps or desktop apps. I have been looking around to find a simple and easy way of creating interactive prototypes for web applications. I'd like to use a t

  • Mocks or real classes? 2008-10-07

    This question already has an answer here: When should I mock? 6 answers Classes that use other classes (as members, or as arguments to methods) need instances that behave properly for unit test. If you have these classes available and they introduce

  • What does AJAX Push have to pay to get web pages more interactive? 2011-05-22

    (Sorry if the topic isn't titled properly, would appreciate if somebody helps me to make it more related to what I explain below). Recently I feel very interested in getting to know AJAX push and all its basic ideas. I know AJAX push technique makes

  • How can I mock an AsyncResponder and the resultant handler functions on a mocked object using FlexUnit 4 and mockolate 2012-03-07

    I am attempting to write some unit tests for a class I am writing in Flex 4.5.1 using FlexUnit 4 and Mockolate for my testing and mocking framework respectively. I am using as3-signals for my custom events. The functionality that I am writing and tes

  • Test-driven development of JavaScript web frontends 2011-04-01

    This might sound a little dumb, but I'm actually a bit confused how to approach JavaScript testing for web frontends. As far as I'm concerned, the typical 3-tier architecture looks like this: Database tier Application tier Client tier 1 is of no conc

  • Deploying software on compromised machines 2010-05-09

    I've been involved in a discussion about how to build internet voting software for a general election. We've reached a general consensus that there exist plenty of secure methods for two way authentication and communication. However, someone came alo

  • How would I implement COMET for my (Flash) browser-based game? 2010-07-09

    I am currently trying to write a browser-based game that allows multiple users to navigate in a 2D map. It's RPG based and requires COMET as a technique for two users appearing and interacting on one screen, should they happen to appear in the same f

  • Unable to call an ASP.NET user control from another 2010-07-30

    I wish to access a user control's public property from another user control. But, I am not able to access one user control within another. Even after using the namespace 'ASP', I am only able to see the reference to the current user control. However,

  • Is there a Delphi equivalent to Java's PermissionManager or AccessController classes? 2012-02-13

    Are there any classes (free, open source or commercial) that perform access control similar to what Java's AccessController does? I want to create a dynamic set of policies that can be changed at runtime. But, I want to avoid having to code if Allowe

  • How do I change defaults for marking text - to move, delete, etc.? 2012-07-29

    In previous windows versions, you could control how you mark text. e.g. I'm used to highlighting the text, then clicking to "set" it before moving or deleting the text. --------------Solutions------------- Do you remember how, specifically, you set t

  • Recording interaction on an inflection point using mocking framework. Moq 2009-07-02

    Updated version of question Hi. My company has a few legacy code bases which I hope to get under test as soon as they migrate to .NET 3.5. I have selected Moq as my Mocking framework (I fell in love with the crisp syntax immediately). One common scen

  • Rhino Mocks - assert no interaction with mock/stub 2010-02-07

    Is it possible to tell that a mock/stub has seen no interaction at all in RhinoMocks. Something along the lines of: logger.AssertNoInteraction(); Which would assert no method has been called on the stubbed logger. This would be a much less tedious th

  • Android Techniques for Mock Data Source in Activity Unit testing 2010-09-07

    I'm fairy new to unit testing and I've been learning how to use the jUnit framework for android(using ActivityInstrumentationTestCase2) but I'm having trouble working out how to I inject a mock data source into and activity, example: In the activiy I

  • Novel Interaction Techniques 2010-09-19

    What exactly are Novel Interaction Techniques, in the context of Human Computer Interaction and Computer Graphics? I have some confusion in mind regarding this, can someone explain with examples? Thanks. --------------Solutions------------- "Novel" i

  • Mocks to verify interaction 2011-06-30

    Typically when I need to mock out a class for testing, I'll use a library such as Rhino Mocks. Here I have a class called MyService that expects a IEmailSender. public class MyService { private readonly IEmailSender sender; public MyService(IEmailSen

  • Mockito - "Wanted but not invoked; However there were other interactions with this mock" error 2011-10-31

    I'm verifying that a function was called using Mockito, but Mockito is telling me that the function I'm verifying was never called and that other functions were called. But it seems to me that I'm calling the right function... Here's the stack trace

  • Ruby Unit testing techniques, Mocking and Stubbing 2011-12-11

    I've been recruited as a SW Dev and I'm trying to get into Unit Testing with RSPEC and RR in ruby but having an hard time deciding on a specific strategy mainly because I was assigned to write unit tests to a code which is already written. Consider t

  • Interactive PHP shell techniques 2012-03-30

    This is a bit of a vague question I realise, but I've been looking around for interactive PHP shells that let me 'play' with classes, methods and data, such as those that come from a CodeIgniter application. The objective is to end up with something

  • What are some techniques for understanding object interaction 2009-08-21

    When starting a new application what are some ways to decide what objects you will need, what they should do, and how they should interact with each other? Is this a job for a white board, or is it easier to just start coding and move stuff around as

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.132 (s). 11 q(s)