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

How can I raise an event?

1 Answer 133 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Stefan
Top achievements
Rank 1
Stefan asked on 14 Apr 2010, 10:36 AM
I'm trying to mock instances of WebClient. How can I invoke an event, say DownloadDataCompleted?


1 Answer, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 15 Apr 2010, 12:38 PM
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);


Regards,
Mehfuz

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.
Tags
General Discussions
Asked by
Stefan
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Share this question
or