Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var IRepository = new Mock<IRepository>();
- Request request = new Request();
- IRepository.Setup(a => a.Save(request)).Raises(a => a.Created += null, RequestCreatedEventArgs.Empty);
- public class Request
- {
- //...
- }
- public class RequestCreatedEventArgs : EventArgs
- {
- Request Request {get; set;}
- }
- //=======================================
- //You must have sender as a first argument
- //=======================================
- public delegate void RequestCreatedEventHandler(object sender, RequestCreatedEventArgs e);
- public interface IRepository
- {
- void Save(Request request);
- event RequestCreatedEventHandler Created;
- }
- [TestMethod]
- public void Test()
- {
- var repository = new Mock<IRepository>();
- Request request = new Request();
- repository.Setup(a => a.Save(request)).Raises(a => a.Created += null, new RequestCreatedEventArgs());
- bool eventRaised = false;
- repository.Object.Created += (sender, e) =>
- {
- eventRaised = true;
- };
- repository.Object.Save(request);
- Assert.IsTrue(eventRaised);
- }
- class IRepository
- {
- public event THING Created;
- }
- class THING : EventArgs
- {
- public static THING Empty
- {
- get { return new THING(); }
- }
- }
Add Comment
Please, Sign In to add comment