How can I raise an event?

2 posts, 0 answers
  1. Stefan
    Stefan avatar
    3 posts
    Member since:
    May 2009

    Posted 14 Apr 2010 Link to this post

    I'm trying to mock instances of WebClient. How can I invoke an event, say DownloadDataCompleted?

  2. Ricky
    Ricky avatar
    467 posts

    Posted 15 Apr 2010 Link to this post

    Hi Stefan,

    Thanks for your question. Currently, at the moment you can  raise event for a particular method call . For example :

    public class FooArgs : EventArgs
    public string Value { get; set; }

    public interface IExecutor<T>
    event EventHandler<FooArgs> Done;
    event EventHandler Executed;

    string Echo(string s);

    Here , i want to raise en event  when foo.Echo is invoked. In that case, i need to do the following:

    var executor = Mock.Create <IExecutor<int>>();

    Mock.Arrange(() => executor.Echo(Arg.Any<string>()))
    .Raises(() => executor.Done += null, (string s) => new FooArgs {Value = s})
    .Returns( (string s) => s);

    FooArgs args = null;
    executor.Done += (sender, e) => args = e;

    Assert.Equal(executor.Echo("echo"), args.Value);

    But, we wiil also be supporting direct raise event like:

    Mock.ArrangeEvent(()=> myInterface.DownloadDataCompleted += null).Raise();

    This will raise the event for any implementationn with default values for raise method that is called as above or with my own custom values as specified below:

    Mock.ArrangeEvent(()=> myInterface.DownloadDataCompleted +=).Raise(someValue);


    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Back to Top