我正在尝试模拟ServiceStack.OrmLite.OrmLiteReadApi.Select()
扩展方法。
我将Moq与Smocks结合使用,因此我可以模拟扩展方法,因为Moq不能满足此要求。
这是我的代码:
//Arrange
Guid duplicateId = new Guid("d3ae99d2-2b1f-4bde-889d-c99387fccd33");
List<SubBranch> fakeSubBranches = new List<SubBranch>
{
new SubBranch { Id = duplicateId },
new SubBranch { Id = duplicateId }
};
Mock<IDbConnection> dbConnectionMock = new Mock<IDbConnection>();
Smock.Run(context =>
{
context.Setup(() => OrmLiteReadApi.Select<SubBranch>(dbConnectionMock.Object)).Returns(fakeSubBranches);
});
Mock<IDbConnectionFactory> dbConnectionFactoryMock = new Mock<IDbConnectionFactory>();
dbConnectionFactoryMock.Setup(x => x.Open()).Returns(dbConnectionMock.Object);
SubBranchRepository subBranchRepository = new SubBranchRepository
{
DbConnectionFactory = dbConnectionFactoryMock.Object
};
//Act
Action act = () => subBranchRepository.Get(duplicateId);
//Assert
var exception = Assert.Throws<Exception>(act);
Assert.Equal($"Found 2 rows with id = {duplicateId}, expected 1 row.", exception.Message);
我在Smock.Run(context =>
行上遇到以下错误:
System.InvalidOperationException: 'Sequence contains more than one element'
StackTrace:
at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
at Smocks.IL.Resolvers.MethodResolver.FindMethod(IEnumerable`1 candidates, String name, TypeReference[] parameterTypes, Boolean isGeneric, GenericBindingContext bindingContext)
at Smocks.IL.Resolvers.MethodResolver.Resolve(MethodReference methodReference, GenericBindingContext bindingContext)
at Smocks.IL.Resolvers.MethodResolver.Resolve(MethodReference methodReference)
at Smocks.IL.ILGeneratorInstructionVisitor.VisitInlineTok(Instruction instruction, MethodReference methodReference)
at Smocks.IL.InstructionExtensions.VisitInlineTok[T](Instruction instruction, IInstructionVisitor`1 visitor)
at Smocks.IL.InstructionExtensions.Accept[T](Instruction instruction, IInstructionVisitor`1 visitor)
at Smocks.IL.DynamicMethodCompiler.AddInstructions(IILGenerator generator, IEnumerable`1 instructions, Dictionary`2 locals)
at Smocks.IL.DynamicMethodCompiler.Compile[T](TypeReference[] parameters, IEnumerable`1 instructions, IEnumerable`1 variables)
at Smocks.IL.ExpressionDecompiler`1.Decompile(MethodBody body, Instruction instruction, Object target, Int32 expectedStackSize, Int32 stackEntriesToSkip)
at Smocks.IL.ExpressionDecompiler`1.Decompile(MethodBody body, Instruction instruction, Object target)
at Smocks.IL.SetupExtractor.<GetSetupsFromInstructions>d__10.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Smocks.Smock.CreateAssemblyRewriter(Delegate delegate, Configuration configuration)
at Smocks.Smock.RunAction(Action`1 action, Configuration configuration)
at Smocks.Smock.Run(Configuration configuration, Action`1 action)
at Smocks.Smock.Run(Action`1 action)
at Abc.Validation.ServiceInterface.Tests.SubBranchRepositoryTests.When_duplicate_ids_present_expect_exception() in C:\SubBranchRepositoryTests.cs:line 32
如何成功模拟Select()
扩展方法?