C# 当安装程序有回调时,Moq抛出NullReferenceException异常

C# 当安装程序有回调时,Moq抛出NullReferenceException异常,c#,unit-testing,mocking,moq,assert,C#,Unit Testing,Mocking,Moq,Assert,我有一个单元测试。当我为接口函数的设置定义回调函数并在调用上调用Verify时,Moq抛出了NullReferenceException异常,这让我非常困惑 更新: 我忘了提到重要的部分。 当我一个接一个地运行测试时,一切正常,但当我运行所有测试时,此测试失败 例外情况: System.NullReferenceException : Object reference not set to an instance of an object. at Moq.MethodCall.Matche

我有一个单元测试。当我为接口函数的设置定义回调函数并在调用上调用Verify时,Moq抛出了
NullReferenceException
异常,这让我非常困惑

更新: 我忘了提到重要的部分。 当我一个接一个地运行测试时,一切正常,但当我运行所有测试时,此测试失败

例外情况:

System.NullReferenceException : Object reference not set to an instance of an object.
   at Moq.MethodCall.Matches(ICallContext call)
   at System.Linq.Enumerable.WhereListIterator`1.MoveNext()
   at System.Linq.Enumerable.Count[TSource](IEnumerable`1 source)
   at Moq.Mock.VerifyCalls(Interceptor targetInterceptor, MethodCall expected, Expression expression, Times times)
   at Moq.Mock.Verify[T](Mock mock, Expression`1 expression, Times times, String failMessage)
   at Moq.Mock`1.Verify(Expression`1 expression, Times times)
   at LimitTest.LimitEditor.LimitEditorTest.EditActionSetExistingLimit(Boolean add) in
单元测试:

var io= new Mock<ILimitServiceIO>(MockBehavior.Strict);
var LimitServiceIO= new MemoryLimitServiceIO();

... different setups...

io.Setup(x => x.PersistActionSet(It.Is<string>(id=>id==companyId), It.IsAny<IList<CsiNotificationActionSet>>()))
                .Callback((string compid, IList<CsiNotificationActionSet> sets) =>LimitServiceIO.PersistActionSet(compid, sets));

... Do some actions...

//This line throws exception
mockIo.Verify(x => x.PersistActionSet(measure.CompanyId,
            It.IsAny<IList<CsiNotificationActionSet>>()), Times.Once());

Assert.AreEqual(2,LimitServiceIO.ActionSets.First(acs => acs.Id == ACTION_SET_A1).Actions.Count,
                "Number of Actions does not match");

非常感谢任何关于它发生的原因的想法或指点。

为子孙后代和其他突然遇到奇怪错误的人。
在测试中设置STAThread属性解决了这个问题

您能给我们显示您要覆盖的原始
PersistActionSet
的签名吗?另外,您似乎在
回调之前缺少
.Verifiable()
@zaitsman这是签名:void peristActionSet(string companyId,IList actionset);不是真的凌驾于任何事情之上implementation@zaitsman为什么需要在回调之前使用.Verifiable()?@OldFox Moq版本是4.8.2,我使用mockIo来验证调用。LimitServiceIO具有硬编码对象,实际上可以检索这些对象。完整的代码是大量的代码,所以尝试给出最重要的。为什么严格的模仿是一种不好的做法,有没有关于更好的做法的建议/文章?
void PersistActionSet(string companyId, IList<CsiNotificationActionSet> actionSets)