使用 JustMock:
我不确定为什么我对静态方法的安排调用试图执行原始方法。
[Test]
public void RunCompleteDivxJob_Negative_Exception()
{
DebugView.IsTraceEnabled = true;
var mediaId = 2000999;
var mediaTypeId = (int)CmtMediaType.Video;
var queueCmtId = 9999;
var cmtFileType = CmtFileType.SourceMediaFile;
var statusError = CmtQueueStatus.Error;
var exception = new Exception("test");
// ARRANGE
var encodeJobStateMachineManager = Mock.Create<EncodeJobStateMachineManager>(Behavior.CallOriginal);
var logger = Mock.Create<Logger>(Behavior.CallOriginal, typeof(EncodeJobStateMachineManager));
var inst = new PrivateAccessor(encodeJobStateMachineManager);
inst.SetProperty("_log", logger);
var createCompleteJobCalled = false;
Mock.Arrange(() => DivxEncodeJob.CreateCompleteJob(mediaId, mediaTypeId, queueCmtId, cmtFileType))
.DoInstead(() => createCompleteJobCalled = true);
Mock.Arrange(() => encodeJobStateMachineManager.EncodeJob.Submit()).Throws(exception).MustBeCalled();
logger.Arrange(x => x.Error(Arg.AnyString, exception)).MustBeCalled();
//Mock.SetupStatic(typeof(QueueDAO));
var updateQueueStatusCalled = false;
Mock.Arrange(() => QueueDAO.UpdateQueueStatus(queueCmtId, statusError)).DoInstead(() => updateQueueStatusCalled = true);
// ACT
encodeJobStateMachineManager.RunCompleteDivxJob(mediaId, mediaTypeId, queueCmtId, cmtFileType);
// ASSERT
Mock.Assert(encodeJobStateMachineManager);
Assert.IsTrue(createCompleteJobCalled);
Assert.IsTrue(updateQueueStatusCalled);
}
第一个静态调用是正确地模拟方法调用:
Mock.Arrange(() => DivxEncodeJob.CreateCompleteJob(mediaId, mediaTypeId, queueCmtId, cmtFileType))
.DoInstead(() => createCompleteJobCalled = true);
但是第二个静态调用正在执行原始代码:
Mock.Arrange(() => QueueDAO.UpdateQueueStatus(queueCmtId, statusError)).DoInstead(() => updateQueueStatusCalled = true);
这些基本上被称为完全相同。那么为什么第一个按预期工作而第二个没有呢?