Created
February 2, 2014 12:27
-
-
Save podhmo/8767662 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using NUnit.Framework; | |
using System; | |
namespace mockSample | |
{ | |
public class AlarmEventArgs : EventArgs | |
{ | |
private readonly int nrings = 0; | |
private readonly bool snoozePressed = false; | |
public AlarmEventArgs (bool snoozePressed, int nrings) | |
{ | |
this.snoozePressed = snoozePressed; | |
this.nrings = nrings; | |
} | |
} | |
public class AlarmClock | |
{ | |
public event AlarmEventHandler Alarm; | |
protected virtual void OnAlarm (AlarmEventArgs e) | |
{ | |
if (this.Alarm != null) { | |
this.Alarm (this, e); | |
} | |
} | |
public void Start () | |
{ | |
OnAlarm (new AlarmEventArgs (false, 10)); | |
} | |
} | |
public delegate void AlarmEventHandler (object sender, AlarmEventArgs e); | |
public interface IAlarmUse | |
{ | |
event AlarmEventHandler AlarmEvent; | |
void OnAlarm(int n); | |
} | |
[TestFixture ()] | |
public class QuickStart2 | |
{ | |
class Foo : IAlarmUse | |
{ | |
public event AlarmEventHandler AlarmEvent; | |
public void OnAlarm (int n) | |
{ | |
if (this.AlarmEvent != null) { | |
this.AlarmEvent (this, new AlarmEventArgs (false, n)); | |
} | |
} | |
} | |
[Test ()] | |
public void TestCaseActual () | |
{ | |
var foo = new Foo (); | |
var result = false; | |
foo.AlarmEvent += (object sender, AlarmEventArgs e) => { | |
result = true; | |
}; | |
Assert.IsFalse (result); | |
foo.OnAlarm (0); | |
Assert.IsTrue (result); | |
} | |
[Test ()] | |
public void TestCaseMock () | |
{ | |
var result = false; | |
var moq = new Moq.Mock<IAlarmUse> (); | |
moq.Object.AlarmEvent += (object o, AlarmEventArgs e) => { | |
result = true; | |
}; | |
moq.Setup(m => m.OnAlarm(0)).Callback (() => { | |
moq.Raise(m => m.AlarmEvent += null, new AlarmEventArgs(false, 1)); | |
}); | |
Assert.IsFalse(result); | |
moq.Object.OnAlarm (0); | |
Assert.IsTrue (true); | |
moq.VerifyAll (); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment