当前位置: 首页>>代码示例>>C#>>正文


C# Mock.Protected方法代码示例

本文整理汇总了C#中Mock.Protected方法的典型用法代码示例。如果您正苦于以下问题:C# Mock.Protected方法的具体用法?C# Mock.Protected怎么用?C# Mock.Protected使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Mock的用法示例。


在下文中一共展示了Mock.Protected方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: AllowMatchersForArgs

        public void AllowMatchersForArgs()
        {
            var mock = new Mock<FooBase>();

            mock.Protected()
                .Setup<string>("StringArg", ItExpr.IsNull<string>())
                .Returns("null");

            Assert.Equal("null", mock.Object.DoStringArg(null));

            mock.Protected()
                .Setup<string>("StringArg", ItExpr.Is<string>(s => s == "bar"))
                .Returns("baz");

            Assert.Equal("baz", mock.Object.DoStringArg("bar"));

            mock = new Mock<FooBase>();

            mock.Protected()
                .Setup<string>("StringArg", ItExpr.Is<string>(s => s.Length >= 2))
                .Returns("long");
            mock.Protected()
                .Setup<string>("StringArg", ItExpr.Is<string>(s => s.Length < 2))
                .Returns("short");

            Assert.Equal("short", mock.Object.DoStringArg("f"));
            Assert.Equal("long", mock.Object.DoStringArg("foo"));

            mock = new Mock<FooBase>();
            mock.CallBase = true;

            mock.Protected()
                .Setup<string>("TwoArgs", ItExpr.IsAny<string>(), 5)
                .Returns("done");

            Assert.Equal("done", mock.Object.DoTwoArgs("foobar", 5));
            Assert.Equal("echo", mock.Object.DoTwoArgs("echo", 15));

            mock = new Mock<FooBase>();
            mock.CallBase = true;

            mock.Protected()
                .Setup<string>("TwoArgs", ItExpr.IsAny<string>(), ItExpr.IsInRange(1, 3, Range.Inclusive))
                .Returns("inrange");

            Assert.Equal("inrange", mock.Object.DoTwoArgs("foobar", 2));
            Assert.Equal("echo", mock.Object.DoTwoArgs("echo", 4));
        }
开发者ID:RajOpteamix,项目名称:moq,代码行数:48,代码来源:ProtectedMockFixture.cs

示例2: Can_Execute_FFmpegComd

		public void Can_Execute_FFmpegComd() {
			// Arrange
			var ffmpegCmdMock = new Mock<FFmpegCommand>();
			ffmpegCmdMock.Protected().Setup("manipulateWithProcess", ItExpr.IsAny<Process>());

			string output = null;
			string error = null;
			ffmpegCmdMock.Protected()
				.Setup("processResult", ItExpr.IsAny<string>(), ItExpr.IsAny<string>())
				.Callback((string o, string e) => {
				          	output = o;
							error = e;
				          });

			FFmpegCommand ffmpegCmd = ffmpegCmdMock.Object;

			// Act
			ffmpegCmd.Execute();

			// Assert
			ffmpegCmdMock.Protected().Verify("manipulateWithProcess", Times.Once(), ItExpr.IsAny<Process>());

			ffmpegCmdMock.Protected().Verify("processResult", Times.Once(), ItExpr.IsAny<string>(), ItExpr.IsAny<string>());
			Assert.IsNotNull(output);
			Assert.IsTrue(output.Contains("built on"));
			Console.WriteLine("Output: ");
			Console.Write(Enumerable.Repeat("-", 20).Aggregate((sum, element) => sum + element) + Environment.NewLine);
			Console.WriteLine(output);

			Assert.IsNotNull(error);
			Console.WriteLine("Error: ");
			Console.Write(Enumerable.Repeat("-", 20).Aggregate((sum, element) => sum + element) + Environment.NewLine);
			Console.WriteLine(error);
		}
开发者ID:ztepsic,项目名称:ZTepsic.FFmpeg,代码行数:34,代码来源:FFmpegCommandTests.cs

示例3: InitMethodMustCallProtectedInit

 public void InitMethodMustCallProtectedInit()
 {
     var presenter = new Mock<PresenterBase<object>>(MockBehavior.Strict);
     presenter.Protected().Setup("Init");
     presenter.Object.Init(new object());
     presenter.Protected().Verify("Init", Times.Once());
 }
开发者ID:gergelykoncz,项目名称:Enterprise.MVP,代码行数:7,代码来源:PresenterBaseTests.cs

示例4: HttpRouteCollection_Dispose_UniquifiesHandlers

        public void HttpRouteCollection_Dispose_UniquifiesHandlers()
        {
            // Arrange
            var collection = new HttpRouteCollection();

            var handler1 = new Mock<HttpMessageHandler>();
            handler1.Protected().Setup("Dispose", true).Verifiable();

            var handler2 = new Mock<HttpMessageHandler>();
            handler1.Protected().Setup("Dispose", true).Verifiable();

            var route1 = new Mock<IHttpRoute>();
            route1.SetupGet(r => r.Handler).Returns(handler1.Object);

            var route2 = new Mock<IHttpRoute>();
            route2.SetupGet(r => r.Handler).Returns(handler1.Object);

            var route3 = new Mock<IHttpRoute>();
            route3.SetupGet(r => r.Handler).Returns(handler2.Object);

            collection.Add("route1", route1.Object);
            collection.Add("route2", route2.Object);
            collection.Add("route3", route3.Object);

            // Act
            collection.Dispose();

            // Assert
            handler1.Protected().Verify("Dispose", Times.Once(), true);
            handler2.Protected().Verify("Dispose", Times.Once(), true);
        }
开发者ID:KevMoore,项目名称:aspnetwebstack,代码行数:31,代码来源:HttpRouteCollectionTest.cs

示例5: TryConvertThrowsPlcException4Test

        public void TryConvertThrowsPlcException4Test()
        {
            Mock<BaseFromStreamConverter<int>> target = new Mock<BaseFromStreamConverter<int>>();
            target.Protected().Setup<bool>("CheckParameter", ItExpr.IsAny<string>()).Returns(true);
            target.Protected().Setup<int>("GetObject", ItExpr.IsAny<string>()).Throws(new PlcException());

            target.Object.TryConvert("0000");
        }
开发者ID:bashocz,项目名称:Mp1PlcModule,代码行数:8,代码来源:BaseFromStreamConverterTest.cs

示例6: TryConvertTest

        public void TryConvertTest()
        {
            Mock<BaseFromStreamConverter<int>> target = new Mock<BaseFromStreamConverter<int>>();
            target.Protected().Setup<bool>("CheckParameter", ItExpr.IsAny<string>()).Returns(true);
            target.Protected().Setup<int>("GetObject", ItExpr.IsAny<string>()).Returns(1);

            Assert.AreEqual<int>(1, target.Object.TryConvert("0000"));
        }
开发者ID:bashocz,项目名称:Mp1PlcModule,代码行数:8,代码来源:BaseFromStreamConverterTest.cs

示例7: TryConvertThrowsPlcException5Test

        public void TryConvertThrowsPlcException5Test()
        {
            Mock<BaseToStreamConverter<int>> target = new Mock<BaseToStreamConverter<int>>();
            target.Protected().Setup<bool>("CheckParameter", ItExpr.IsAny<int>()).Returns(true);
            target.Protected().Setup<int>("GetLength", ItExpr.IsAny<int>()).Returns(1);
            target.Protected().Setup<string>("GetStream", ItExpr.IsAny<int>()).Throws(new PlcException());

            target.Object.TryConvert(0);
        }
开发者ID:bashocz,项目名称:Mp1PlcModule,代码行数:9,代码来源:BaseToStreamConverterTest.cs

示例8: Command_line_is_added_after_ini_file_to_configuration

        public void Command_line_is_added_after_ini_file_to_configuration()
        {
            var mock = new Mock<Program>() { CallBase = true };

            mock.Protected()
                .Setup<IConfigurationSourceContainer>("CreateConfiguration")
                .Returns(
                    () =>
                        {
                            var configurationMock = new Mock<IConfigurationSourceContainer>();
                            var callCount = 0;

                            configurationMock
                                .Setup(m => m.Add(It.IsAny<IConfigurationSource>()))
                                .Callback<IConfigurationSource>(
                                    s =>
                                    {
                                        if (s is IniFileConfigurationSource)
                                        {
                                            Assert.Equal(0, callCount);
                                        }
                                        else
                                        {
                                            Assert.True(s is CommandLineConfigurationSource);
                                            Assert.Equal(1, callCount);
                                        }

                                        callCount++;
                                    });

                            return configurationMock.Object;
                        });

            mock.Protected()
                .Setup<MigrationTool>("CreateMigrationTool")
                .Returns(
                    () =>
                        {
                            var toolMock = new Mock<MigrationTool>() { CallBase = true };

                            toolMock.Setup(m => m.CreateMigration(It.IsAny<IConfigurationSourceContainer>()));

                            return toolMock.Object;
                        });

            var args
                = new[]
                      {
                          "--ConfigFile=MyConfigFile.ini",
                          "--MigrationName=MyMigration"
                      };

            mock.Object.CreateMigration(args);

            mock.Protected().Verify<MigrationTool>("CreateMigrationTool", Times.Once());
            mock.Protected().Verify<IConfigurationSourceContainer>("CreateConfiguration", Times.Once());
        }
开发者ID:Nyaoso,项目名称:EntityFramework,代码行数:57,代码来源:ProgramTest.cs

示例9: TryConvertTest

        public void TryConvertTest()
        {
            Mock<BaseToStreamConverter<int>> target = new Mock<BaseToStreamConverter<int>>();
            target.Protected().Setup<bool>("CheckParameter", ItExpr.IsAny<int>()).Returns(true);
            target.Protected().Setup<int>("GetLength", ItExpr.IsAny<int>()).Returns(1);
            target.Protected().Setup<string>("GetStream", ItExpr.IsAny<int>()).Returns("0000");

            PlcWriteStream result = target.Object.TryConvert(0);
            Assert.AreEqual<int>(1, result.Length);
            Assert.AreEqual<string>("0000", result.Stream);
        }
开发者ID:bashocz,项目名称:Mp1PlcModule,代码行数:11,代码来源:BaseToStreamConverterTest.cs

示例10: should_call_protected_method

        public void should_call_protected_method()
        {
            var mock = new Mock<OneClass>();

            mock.Protected().Setup("OneProtectedMethod").Callback(() => Console.WriteLine("Goodbye"));

            var o = new OneClass();

            o.DoMethod();

            mock.Protected().Verify("OneProtectedMethod", Times.Once());
        }
开发者ID:Marimout,项目名称:TestMoq,代码行数:12,代码来源:TestOneClass.cs

示例11: AuthenticateWithInvalidImplementation

        public void AuthenticateWithInvalidImplementation()
        {
            var syncManagerMock = new Mock<SyncManager>(authenticatorMock.Object);
            syncManagerMock
                .Protected()
                .Setup("OnException", ItExpr.IsAny<AuthenticatedTicket>(), ItExpr.IsAny<Exception>());

            syncManagerMock.CallBase = true;
            syncManagerMock.Object.Authenticate("user", "password");

            syncManagerMock
                .Protected()
                .Verify("OnException", Times.Once(), ItExpr.Is<AuthenticatedTicket>(m => m == null), ItExpr.Is<Exception>(m => m.Message == "GetAuthenticationFromLogin must return a ticket."));
        }
开发者ID:admere,项目名称:quickbooks-sync,代码行数:14,代码来源:SyncManagerTests.cs

示例12: TestAppendTableData

 public void TestAppendTableData()
 {
     var tableDesc = new Mock<TableDataDesc>(It.IsAny<Table>());
       tableDesc.Protected().Setup<IEnumerable<string>>("GetSmoTableDataScript", ItExpr.IsAny<Table>())
     .Returns(Resources.TableScript.Split(Environment.NewLine.ToCharArray()));
       tableDesc.Protected().Setup<IEnumerable<string>>("GetSmoTablePrimaryColumns", ItExpr.IsAny<Table>())
     .Returns(new string[] { });
       tableDesc.CallBase = true;
       var builder = new TableScriptBuilder();
       builder.Append(tableDesc.Object);
       var script = builder.GetScript();
       Trace.WriteLine(script);
       Assert.AreEqual(Resources.MergeTableScript, script);
 }
开发者ID:popovegor,项目名称:tools,代码行数:14,代码来源:TableDataScriptBuilderTestFixture.cs

示例13: CreateBusinessConfiguratorFactory

        private BusinessConfiguratorFactory CreateBusinessConfiguratorFactory()
        {
            m_configuratorMock = new Mock<BusinessConfigurator>();
            var configurators = new List<Tuple<CrmPluginEvent, BusinessConfigurator>>()
            {
                new Tuple<CrmPluginEvent, BusinessConfigurator>(TestPluginEvent, m_configuratorMock.Object)
            };
            var factoryMock = new Mock<BusinessConfiguratorFactory> { CallBase = true };
            factoryMock.Protected().Setup<List<Tuple<CrmPluginEvent, BusinessConfigurator>>>("CreateConfiguratorEntries")
                                   .Returns(configurators);
            factoryMock.Protected().Setup<string>("GetFactoryName").Returns(TestFactoryName);

            return factoryMock.Object;
        }
开发者ID:sev15,项目名称:CrmPlugins,代码行数:14,代码来源:BusinessConfiguratorFactoryTests.cs

示例14: HandleQuestion_ShouldUseTheInjectedStateService

        public void HandleQuestion_ShouldUseTheInjectedStateService()
        {
            // Arrange
            var question = _fixture.Create<IThermometerQuestion>();
            var stateService = _fixture.Create<IStateService>();


            var sut = new Mock<StateServiceBaseHandler>(stateService) {CallBase = true};
            sut.Protected().Setup("HandleQuestion", question, stateService);

            var func = sut.Object.Handler;
            var result = func(question);

            sut.Protected().Verify("HandleQuestion", Times.Once(), question, stateService);
        }
开发者ID:junshao,项目名称:Medidata.Cloud.Thermometer.RaveCommon,代码行数:15,代码来源:StateServiceBaseHandlerTests.cs

示例15: ExecuteSql_dispatches_to_interceptors

        public void ExecuteSql_dispatches_to_interceptors()
        {
            var mockCommand = new Mock<DbCommand>();
            mockCommand.Setup(m => m.ExecuteNonQuery()).Returns(2013);

            var mockConnection = new Mock<DbConnection>();
            mockConnection.Protected().Setup<DbCommand>("CreateDbCommand").Returns(mockCommand.Object);

            var mockTransaction = new Mock<DbTransaction>(MockBehavior.Strict);
            mockTransaction.Protected().Setup<DbConnection>("DbConnection").Returns(mockConnection.Object);

            var migrator = new DbMigrator();
            var statement = new MigrationStatement
            {
                Sql = "Some Sql"
            };

            var providerFactoryServiceMock = new Mock<IDbProviderFactoryResolver>();
            providerFactoryServiceMock.Setup(m => m.ResolveProviderFactory(It.IsAny<DbConnection>()))
                .Returns(FakeSqlProviderFactory.Instance);
            MutableResolver.AddResolver<IDbProviderFactoryResolver>(k => providerFactoryServiceMock.Object);
            var mockInterceptor = new Mock<DbCommandInterceptor> { CallBase = true };
            DbInterception.Add(mockInterceptor.Object);
            var transactionInterceptorMock = new Mock<IDbTransactionInterceptor>();
            DbInterception.Add(transactionInterceptorMock.Object);
            try
            {
                new MigratorLoggingDecorator(migrator, new Mock<MigrationsLogger>().Object)
                    .ExecuteSql(mockTransaction.Object, statement, new DbInterceptionContext());
            }
            finally
            {
                MutableResolver.ClearResolvers();
                DbInterception.Remove(mockInterceptor.Object);
                DbInterception.Remove(transactionInterceptorMock.Object);
            }

            mockInterceptor.Verify(m => m.NonQueryExecuting(mockCommand.Object, It.IsAny<DbCommandInterceptionContext<int>>()), Times.Once());
            mockInterceptor.Verify(m => m.NonQueryExecuted(mockCommand.Object, It.IsAny<DbCommandInterceptionContext<int>>()), Times.Once());

            transactionInterceptorMock.Verify(
                m => m.ConnectionGetting(It.IsAny<DbTransaction>(), It.IsAny<DbTransactionInterceptionContext<DbConnection>>()),
                Times.Exactly(2));
            transactionInterceptorMock.Verify(
                m => m.ConnectionGot(It.IsAny<DbTransaction>(), It.IsAny<DbTransactionInterceptionContext<DbConnection>>()),
                Times.Exactly(2));
            mockTransaction.Protected().Verify<DbConnection>("DbConnection", Times.Exactly(2));
        }
开发者ID:jesusico83,项目名称:Telerik,代码行数:48,代码来源:MigratorLoggingDecoratorTests.cs


注:本文中的Mock.Protected方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。