Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 检查FluentAssertion异常语法中的返回值_C#_Nunit_Nsubstitute_Fluent Assertions - Fatal编程技术网

C# 检查FluentAssertion异常语法中的返回值

C# 检查FluentAssertion异常语法中的返回值,c#,nunit,nsubstitute,fluent-assertions,C#,Nunit,Nsubstitute,Fluent Assertions,我想通过FluentAssertion语法检查方法的返回值。请考虑以下片段: public interface IFoo { Task<int> DoSomething(); } public class Bar { private readonly IFoo _foo; private static int _someMagicNumber = 17; public Bar(IFoo foo) { _foo = foo;

我想通过FluentAssertion语法检查方法的返回值。请考虑以下片段:

public interface IFoo
{
    Task<int> DoSomething();
}

public class Bar
{
    private readonly IFoo _foo;
    private static int _someMagicNumber = 17;

    public Bar(IFoo foo)
    {
        _foo = foo;
    }

    public async Task<int> DoSomethingSmart()
    {
        try
        {
            return await _foo.DoSomething();
        }
        catch
        {
            return _someMagicNumber;
        }
    }
}

[TestFixture]
public class BarTests
{
    [Test]
    public async Task ShouldCatchException()
    {
        // Arrange
        var foo = Substitute.For<IFoo>();
        foo.DoSomething().Throws(new Exception());
        var bar = new Bar(foo);
        Func<Task> result = () => bar.DoSomethingSmart();

        // Act-Assert
        await result.Should().NotThrowAsync();
    }

    [Test]
    public async Task ShouldReturnDefaultValueWhenExceptionWasThrown()
    {
        // Arrange
        var foo = Substitute.For<IFoo>();
        foo.DoSomething().Throws(new Exception());
        var bar = new Bar(foo);

        // Act
        var result = await bar.DoSomethingSmart();

        // Assert
        result.Should().Be(17);
    }
}
公共接口IFoo
{
任务DoSomething();
}
公共类酒吧
{
私有只读iFooFoo;
私有静态int_someMagicNumber=17;
公共酒吧(IFoo-foo)
{
_foo=foo;
}
公共异步任务DoSomethingSmart()
{
尝试
{
返回等待_foo.DoSomething();
}
抓住
{
返回_someMagicNumber;
}
}
}
[测试夹具]
公营易货者
{
[测试]
公共异步任务ShouldCatchException()
{
//安排
var foo=Substitute.For();
foo.DoSomething().Throws(新异常());
var bar=新的bar(foo);
Func result=()=>bar.DoSomethingSmart();
//行动主张
等待结果.Should().NotThrowAsync();
}
[测试]
当抛出ception()时,公共异步任务应返回DefaultValueWhen
{
//安排
var foo=Substitute.For();
foo.DoSomething().Throws(新异常());
var bar=新的bar(foo);
//表演
var result=wait bar.DoSomethingSmart();
//断言
result.Should()为(17);
}
}
我的目标是将这两个测试合并到新的测试中,但我希望保留流畅的断言检查:
result.Should().NotThrowAsync()


因此,我的问题是如何在第一个测试中检查返回值在我的示例中是否为
17

当前版本的Fluent断言(5.5.3)没有区分
Func
Func
。 这两种类型都由
asyncFunctionsSections
处理,它将其分配给
Func
,从而释放
任务的返回值

避免这种情况的一种方法是将返回值赋给局部变量

[Test]
public async Task ShouldCatchException()
{
    // Arrange
    var foo = Substitute.For<IFoo>();
    foo.DoSomething().Throws(new Exception());
    var bar = new Bar(foo);

    // Act
    int? result = null;
    Func<Task> act = async () => result = await bar.DoSomethingSmart();

    // Act-Assert
    await act.Should().NotThrowAsync();
    result.Should().Be(17);
}
[测试]
公共异步任务ShouldCatchException()
{
//安排
var foo=Substitute.For();
foo.DoSomething().Throws(新异常());
var bar=新的bar(foo);
//表演
int?结果=null;
Func act=async()=>result=wait bar.DoSomethingSmart();
//行动主张
等待act.Should().NotThrowAsync();
result.Should()为(17);
}

我已经在Fluent断言问题跟踪器上创建了一个。Fluent断言的当前版本(5.5.3)没有区分
Func
Func
。 这两种类型都由
asyncFunctionsSections
处理,它将其分配给
Func
,从而释放
任务的返回值

避免这种情况的一种方法是将返回值赋给局部变量

[Test]
public async Task ShouldCatchException()
{
    // Arrange
    var foo = Substitute.For<IFoo>();
    foo.DoSomething().Throws(new Exception());
    var bar = new Bar(foo);

    // Act
    int? result = null;
    Func<Task> act = async () => result = await bar.DoSomethingSmart();

    // Act-Assert
    await act.Should().NotThrowAsync();
    result.Should().Be(17);
}
[测试]
公共异步任务ShouldCatchException()
{
//安排
var foo=Substitute.For();
foo.DoSomething().Throws(新异常());
var bar=新的bar(foo);
//表演
int?结果=null;
Func act=async()=>result=wait bar.DoSomethingSmart();
//行动主张
等待act.Should().NotThrowAsync();
result.Should()为(17);
}

我已经在Fluent断言问题跟踪器上创建了一个问题跟踪器。

您正在捕获并接受异常,因此无需检查。一旦捕获到异常,它将返回17Yes,因为这是一个简化的示例。本规范的目的是证明我的要求。是的,但第一次测试没有任何目的。第二个测试的成功完成意味着异常已被捕获,因此无需合并或检查您正在捕获并吞咽异常,因此无需检查。一旦捕获到异常,它将返回17Yes,因为这是一个简化的示例。本规范的目的是证明我的要求。是的,但第一次测试没有任何目的。第二个测试的成功完成意味着异常被捕获,因此没有任何东西需要合并或检查