好吧,我正在尝试针对我为录制会话创建的包装器对 NAudio 进行单元测试,这是启动和停止录制会话的代码...
public void StartRecording(string claimNo, string ip_no, string ip_name)
{
    if (this.IsRecording)
    {
        return;
    }
    this.Recordings.Add(new RecordingTrack(claimNo, ip_no, ip_name));
    if (this.MicrophoneLevel == default(float))
    {
        this.MicrophoneLevel = .75f;
    }
    _aggregator.Reset();
    _input = new WaveIn();
    _input.WaveFormat = _waveFormat;
    _input.DataAvailable += (s, args) =>
        {
            _writer.Write(args.Buffer, 0, args.BytesRecorded);
            byte[] buffer = args.Buffer;
            for (int index = 0; index < args.BytesRecorded; index += 2)
            {
                short sample = (short)((buffer[index + 1] << 8) | buffer[index + 0]);
                float sample32 = sample / 32768f;
                _aggregator.Add(sample32);
            }
            if (this.DataAvailable != null)
            {
                this.DataAvailable(s, args);
            }
            if (!this.IsRecording)
            {
                _writer.Close();
                _writer.Dispose();
                _writer = null;
            }
        };
    _input.RecordingStopped += (s, args) =>
        {
            _input.Dispose();
            _input = null;
            if (this.RecordingStopped != null)
            {
                this.RecordingStopped(s, args);
            }
        };
    _writer = new WaveFileWriter(this.CurrentRecording.FileName, _input.WaveFormat);
    _input.StartRecording();
    this.IsRecording = true;
}
public void StopRecording()
{
    if (!this.IsRecording)
    {
        return;
    }
    this.CurrentRecording.Stop();
    this.IsRecording = false;
    _input.StopRecording();
}
...下面是我的单元测试。我正在使用 aManualResetEvent来断言被触发的事件是否成功,并且它是这样声明的......
private ManualResetEvent _eventRaised = new ManualResetEvent(false);
...但是,问题是下面的测试只是锁定并且永远不会触发事件。您能否确认问题在于WaitOne不允许事件触发,因为它锁定了同一个线程?
bool success = false;
_eventRaised.Reset();
var target = new RecordingSession();
target.StartRecording("1", "01", "Test Name");
target.RecordingStopped += (s, args) =>
    {
        success = (target.CurrentRecording.Duration.TotalSeconds > 4);
        _eventRaised.Set();
    };
Thread.Sleep(4000);
target.StopRecording();
_eventRaised.WaitOne();
Assert.IsTrue(success);
如果是这样,你能帮我做这个测试吗?我需要一些启示。
我已经ManualResetEvent多次使用它来测试其他类的事件并且它有效,但这里有些不同。