本文整理汇总了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>());
}
示例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();
});
}
示例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"));
}
示例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."));
}
示例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));
}
示例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"));
}
示例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();
}
示例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"));
}
示例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>());
}
示例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"));
}
示例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"));
}
示例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"));
}
示例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"));
}
示例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>());
}
示例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());
}