Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Hard to know what you are trying to test here, so I can't give you a 100% accurate sample. Your sample code seems to be missing quite a few details... I filled some of the missing bits in, but there are more questions.</p> <p>In any case, a method I use for waiting on events is a semaphore. I like to use AutoResetEvent for this in simple occasions like this.</p> <pre><code>public class Foo { private IService _service; public EventHandler CalculationComplete; public Foo(IService service) { _service = service; _service.DoCompleted += (o,e) =&gt; { Calculated = e.Result; if(CalculationComplete != null) { CalculationComplete(this, new EventArgs()); } }; } public int Calculated; public void CalculateAsync(int param) { _service.DoAsync(param); } } public interface IService { void DoAsync(int param); event EventHandler&lt;DoResultEventArgs&gt; DoCompleted; } public class DoResultEventArgs : EventArgs { public int Result { get; set; } } [TestMethod] public void CalculateAsync_CallsService_CalculatedIsPopulated() { //Arrange Mock&lt;IService&gt; sMock = new Mock&lt;IService&gt;(); sMock.Setup(s =&gt; s.DoAsync(It.IsAny&lt;int&gt;())) .Raises(s =&gt; s.DoCompleted += null, new DoResultEventArgs() { Result = 324 }); Foo foo = new Foo(sMock.Object); AutoResetEvent waitHandle = new AutoResetEvent(false); foo.CalculationComplete += (o,e) =&gt; waitHandle.Set(); //Act foo.CalculateAsync(12); waitHandle.WaitOne(); //Assert Assert.IsEqual(foo.Calculated, 324); } </code></pre> <p>Without more information, this is the best I can do. I hope it was what you were looking for.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload