【发布时间】:2018-04-11 18:23:41
【问题描述】:
我在我的存储库周围进行圆顶单元测试,直到我遇到一个奇怪的错误。环顾四周,看看我是否没有犯已知的错误,我可以简化它并注意到我遇到了同样的错误。 看起来我无法正确模拟 IFindFluent 界面,我想知道我做错了什么。本次测试:
[Fact]
public void Test()
{
var ff = Substitute.For<IFindFluent<string, string>>();
ff.FirstOrDefaultAsync().Returns("asd");
}
正在返回此错误消息:
NSubstitute.Exceptions.CouldNotSetReturnDueToTypeMismatchException : 无法为 IDisposable.Dispose 返回 Task`1 类型的值(预期 输入无效)。
确保在调用替代者后调用 Returns()(对于 例如:mySub.SomeMethod().Returns(value)),而你不是 在 Returns() 中配置其他替代品(例如,避免 这个:mySub.SomeMethod().Returns(ConfigOtherSub()))。
如果您替换为类而不是接口,请检查 对您的替代者的呼叫是在虚拟/抽象成员上。返回 不能为非虚拟/非抽象成员配置值。
正确使用: mySub.SomeMethod().Returns(returnValue);
潜在问题的使用: mySub.SomeMethod().Returns(ConfigOtherSub());而是尝试: var returnValue = ConfigOtherSub(); mySub.SomeMethod().Returns(returnValue);
在 NSubstitute.Core.ConfigureCall.CheckResultIsCompatibleWithCall(IReturn valueToReturn,ICallSpecification 规范)在 NSubstitute.Core.ConfigureCall.SetResultForLastCall(IReturn valueToReturn, MatchArgs matchArgs) 在 NSubstitute.Core.SubstitutionContext.LastCallShouldReturn(IReturn 值,MatchArgs matchArgs)在 CorporateActions.Tests.Persistence.RepositoryTests.Test()
我已经四处搜索,但最常见的错误不适合这个简单的实现。任何想法为什么这不起作用?
【问题讨论】:
-
能把
IFindFluent.FirstOrDefaultAsync的签名发上来吗?还是扩展方法? -
正如 CodeFuller 在下面的答案中指出的那样,它是一种扩展方法。不知道我不能直接模拟它。
标签: c# mongodb unit-testing nsubstitute