Методы расширения OrmLiteReadApi - PullRequest
0 голосов
/ 05 декабря 2018

Я пытаюсь смоделировать метод расширения 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()?

...