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


C# Exception.Should方法代码示例

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


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

示例1: VoidMethodWithThrows

        public void VoidMethodWithThrows(
            IFoo foo,
            Action action,
            Exception exception)
        {
            "Given a fake"
                .x(() => foo = A.Fake<IFoo>());

            "And an action"
                .x(() => action = A.Fake<Action>());

            "When a void method is configured to invoke a delegate twice then throw an exception"
                .x(() =>
                    A.CallTo(() => foo.Bar()).Invokes(action).DoesNothing().Twice()
                        .Then.Throws<InvalidOperationException>());

            "And the method is called 3 times"
                .x(() =>
                {
                    foo.Bar();
                    foo.Bar();
                    exception = Record.Exception(() => foo.Bar());
                });

            "Then the delegate is invoked twice"
                .x(() => A.CallTo(() => action()).MustHaveHappened(Repeated.Exactly.Twice));

            "And the third call throws an exception"
                .x(() => exception.Should().BeAnExceptionOfType<InvalidOperationException>());
        }
开发者ID:adamralph,项目名称:FakeItEasy,代码行数:30,代码来源:ThenSpecs.cs

示例2: ExceptionInStage

        public void ExceptionInStage(TrackedCommand command, Exception exception)
        {
            "Given I have a command"
                .Given(() =>
                {
                    command = new TrackedCommand { A = 1, B = 2 };
                });

            "And a pipeline stage which causes an exception"
                .And(() =>
                {
                    var stageA = new ExceptionPipelineStage {Next = pipeline.IssueCommand};
                    pipeline.SetRoot(stageA);
                });

            "When the command is processed in the pipeline"
                .When(async () =>
                {
                    try
                    {
                        await pipeline.Execute(command);
                    }
                    catch (Exception ex)
                    {
                        exception = ex;
                    }
                });

            "Then the exception is not captured"
                .Then(() =>
                {
                    exception.Should().NotBeNull();
                });
        }
开发者ID:sequin,项目名称:sequin,代码行数:34,代码来源:CommandPipelineFeature.cs

示例3: TryingToGetAnIncorrectlyTypedConfigurationItem

        public static void TryingToGetAnIncorrectlyTypedConfigurationItem(Exception ex)
        {
            "Given a config file with a string named 'foo'"
                .f(() =>
                {
                    using (var writer = new StreamWriter("foo1.csx"))
                    {
                        writer.WriteLine(@"Configurator.Add(""foo"", ""abc"");");
                        writer.Flush();
                    }
                })
                .Teardown(() => File.Delete("foo1.csx"));

            "When I load the file"
                .When(() => Configurator.Load("foo1.csx"));

            "And I try to get an integer named 'foo'"
                .f(() => ex = Record.Exception(() => Configurator.Get<int>("foo")))
                .Teardown(() => Configurator.Unload());

            "Then an exception is thrown"
                .f(() => ex.Should().NotBeNull());

            "And the exception message contains 'foo'"
                .f(() => ex.Message.Should().Contain("foo"));

            "And the exception contains an inner exception"
                .f(() => ex.InnerException.Should().NotBeNull());

            "And the inner exception message contains 'string'"
                .f(() => ex.InnerException.Message.Should().Contain("string"));

            "And the inner exception message contains 'int'"
                .f(() => ex.InnerException.Message.Should().Contain("int"));
        }
开发者ID:rajeshgupthar,项目名称:config-r,代码行数:35,代码来源:TypedConfigurationItemFeature.cs

示例4: DependencyFails

        public static void DependencyFails(ITaskBuilder builder, bool defaultExecuted, Exception ex)
        {
            "And a non-default task which fails"
                .f(c => builder = ScriptCs.Require<Bau>()
                    .Task("non-default").DependsOn("default")
                        .Do(() =>
                        {
                            throw new Exception();
                        }));

            "And a default task which depends on the non-default task"
                .f(c => builder
                    .Task("default").DependsOn("non-default")
                        .Do(() => defaultExecuted = true));

            "When I run the builder"
                .f(() => ex = Record.Exception(() => builder.Run()));

            "Then execution should fail"
                .f(() => ex.Should().NotBeNull());

            "And the default task is not executed"
                .f(() => defaultExecuted.Should().BeFalse());

            "And I am informed that the non-default task was executed"
                .f(() => ex.Message.Should().Contain("'non-default' task failed."));
        }
开发者ID:pgermishuys,项目名称:bau,代码行数:27,代码来源:TaskDependencies.cs

示例5: TryingToGetAnIncorrectlyTypedConfigurationItem

        public static void TryingToGetAnIncorrectlyTypedConfigurationItem(Exception ex)
        {
            "Given a config file with a string named 'foo'"
                .f(() =>
                {
                    // TODO (Adam): add DSL - new TempFile(string path, string content).Using();
                    using (var writer = new StreamWriter("foo1.csx"))
                    {
                        writer.WriteLine(@"Add(""foo"", ""abc"");");
                        writer.Flush();
                    }
                })
                .Teardown(() => File.Delete("foo1.csx"));

            "When I load the file"
                .f(() => Config.Global.LoadScriptFile("foo1.csx"));

            "And I try to get an integer named 'foo'"
                .f(() => ex = Record.Exception(() => Config.Global.Get<int>("foo")));

            "Then an exception is thrown"
                .f(() => ex.Should().NotBeNull());

            "And the exception message contains 'foo'"
                .f(() => ex.Message.Should().Contain("foo"));

            "And the exception message contains the full type name of int"
                .f(() => ex.Message.Should().Contain(typeof(int).FullName));

            "And the exception message contains the full type name of string"
                .f(() => ex.Message.Should().Contain(typeof(string).FullName));
        }
开发者ID:catwarrior,项目名称:config-r,代码行数:32,代码来源:TypedConfigurationItemFeature.cs

示例6: RepeatedAssertion

        public static void RepeatedAssertion(
            IMyInterface fake,
            Exception exception)
        {
            "establish"
                .x(() =>
                    {
                        fake = A.Fake<IMyInterface>(o => o.Strict());
                        ////fake = A.Fake<IMyInterface>();

                        A.CallTo(() => fake.DoIt(Guid.Empty)).Invokes(() => { });

                        fake.DoIt(Guid.Empty);
                        fake.DoIt(Guid.Empty);
                    });

            "when asserting must have happened when did not happen"
                .x(() => exception = Record.Exception(() => A.CallTo(() => fake.DoIt(Guid.Empty)).MustHaveHappened(Repeated.Exactly.Once)));

            "it should throw an expectation exception"
                .x(() => exception.Should().BeAnExceptionOfType<ExpectationException>());

            "it should have an exception message containing the name of the method"
                .x(() => exception.Message.Should().Contain("DoIt"));
        }
开发者ID:huoxudong125,项目名称:FakeItEasy,代码行数:25,代码来源:StrictFakeSpecs.cs

示例7: Should_support_chaining_constraints_with_and

 public void Should_support_chaining_constraints_with_and()
 {
     var someObject = new Exception();
     someObject.Should()
         .BeOfType<Exception>()
         .And
         .NotBeNull();
 }
开发者ID:9swampy,项目名称:fluentassertions,代码行数:8,代码来源:ObjectAssertionSpecs.cs

示例8: ConfiguringSetterWithNull

        public static void ConfiguringSetterWithNull(
            Exception exception)
        {
            "When assignment of a property is configured using a null expression"
                .x(() => exception = Record.Exception(() => A.CallToSet<int>(null)));

            "Then an argument null exception is thrown"
                .x(() => exception.Should().BeAnExceptionOfType<ArgumentNullException>());

            "And the parameter name is 'propertySpecification'"
                .x(() => exception.As<ArgumentNullException>().ParamName.Should().Be("propertySpecification"));
        }
开发者ID:bman61,项目名称:FakeItEasy,代码行数:12,代码来源:ConfiguringPropertySetterSpecs.cs

示例9: ConfiguringNonConfigurableSetter

        public static void ConfiguringNonConfigurableSetter(
            ClassWithInterestingProperties subject,
            Exception exception)
        {
            "Given a Fake with a property that can't be configured"
                .x(() => subject = A.Fake<ClassWithInterestingProperties>());

            "When assignment of the property is configured"
                .x(() => exception = Record.Exception(() => A.CallToSet(() => subject.NonConfigurableProperty).DoesNothing()));

            "Then a fake configuration exception is thrown"
                .x(() => exception.Should().BeAnExceptionOfType<FakeConfigurationException>());
        }
开发者ID:bman61,项目名称:FakeItEasy,代码行数:13,代码来源:ConfiguringPropertySetterSpecs.cs

示例10: NoTasksExist

        public static void NoTasksExist(ITaskBuilder builder, Exception ex)
        {
            "Given no tasks"
                .f(() => builder = ScriptCs.Require<Bau>());

            "When I run the builder"
                .f(() => ex = Record.Exception(() => builder.Run()));

            "Then execution should fail"
                .f(() => ex.Should().NotBeNull());

            "And I am informed that the default task was not found"
                .f(() => ex.Message.Should().Contain("'default' task not found"));
        }
开发者ID:pgermishuys,项目名称:bau,代码行数:14,代码来源:DefaultTask.cs

示例11: OverrideInternalMethod

        public void OverrideInternalMethod(
            TypeWithInternalMethod fake,
            Exception exception)
        {
            "establish"
                .x(() => fake = A.Fake<TypeWithInternalMethod>());

            "when trying to override internal method on type"
                .x(() => exception = Record.Exception(() => A.CallTo(() => fake.InternalMethod()).Returns(17)));

            "it should throw an exception with a message complaining about accessibility"
                .x(() => exception.Should().BeAnExceptionOfType<FakeConfigurationException>()
                             .And.Message.Should().Contain("not accessible to DynamicProxyGenAssembly2"));
        }
开发者ID:JoeStead,项目名称:FakeItEasy,代码行数:14,代码来源:FakingInternalsSpecs.cs

示例12: NonexistentTask

        public static void NonexistentTask(string tag, string code, Baufile baufile, Exception ex)
        {
            var scenario = MethodBase.GetCurrentMethod().GetFullName();

            "Given a baufile containing {0}"
                .f(() => baufile = Baufile.Create(string.Concat(scenario, ".", tag)).WriteLine(code));

            "When I execute a non-existent task"
                .f(() => ex = Record.Exception(() => baufile.Run("non-existent")));

            "Then execution should fail"
                .f(() => ex.Should().NotBeNull());

            "And I am informed that the non-existent task was not found"
                .f(() => ex.Message.Should().ContainEquivalentOf("'non-existent' task not found"));
        }
开发者ID:pgermishuys,项目名称:bau,代码行数:16,代码来源:SpecificTasks.cs

示例13: CompilationFails

        public static void CompilationFails(Baufile baufile, Exception ex)
        {
            var scenario = MethodBase.GetCurrentMethod().GetFullName();

            "Given a baufile containing an unknown name"
                .f(() => baufile = Baufile.Create(scenario).WriteLine(@"x"));

            "When I execute the baufile"
                .f(() => ex = Record.Exception(() => baufile.Run()));

            "Then execution should fail"
                .f(() => ex.Should().NotBeNull());

            "And I am informed that member could not be found"
                .f(() => ex.Message.Should().ContainEquivalentOf("the name 'x' does not exist in the current context"));
        }
开发者ID:pgermishuys,项目名称:bau,代码行数:16,代码来源:ScriptExecution.cs

示例14: CallsToVoidMethodThrows

        public static void CallsToVoidMethodThrows(
            Fake<IFoo> fake,
            Exception exception)
        {
            "Given an unnatural fake"
                .x(() => fake = new Fake<IFoo>());

            "And I configure a void method to throw an exception"
                .x(() => fake.CallsTo(f => f.VoidMethod()).Throws<ArithmeticException>());

            "When I call the method on the faked object"
                .x(() => exception = Record.Exception(() => fake.FakedObject.VoidMethod()));

            "Then it throws the exception"
                .x(() => exception.Should().BeAnExceptionOfType<ArithmeticException>());
        }
开发者ID:TimLovellSmith,项目名称:FakeItEasy,代码行数:16,代码来源:UnnaturalFakeSpecs.cs

示例15: CallsToVoidMethodDoesNothing

        public static void CallsToVoidMethodDoesNothing(
            Fake<AClass> fake,
            Exception exception)
        {
            "Given a strict unnatural fake"
                .x(() => fake = new Fake<AClass>(options => options.Strict()));

            "And I configure a void method to do nothing"
                .x(() => fake.CallsTo(f => f.VoidMethod()).DoesNothing());

            "When I call the method on the faked object"
                .x(() => exception = Record.Exception(() => fake.FakedObject.VoidMethod()));

            "Then it doesn't throw"
                .x(() => exception.Should().BeNull());
        }
开发者ID:TimLovellSmith,项目名称:FakeItEasy,代码行数:16,代码来源:UnnaturalFakeSpecs.cs


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