Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.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# FluentAssertions:如何比较不同名称的属性_C#_Unit Testing_Vs Unit Testing Framework_Fluent Assertions - Fatal编程技术网

C# FluentAssertions:如何比较不同名称的属性

C# FluentAssertions:如何比较不同名称的属性,c#,unit-testing,vs-unit-testing-framework,fluent-assertions,C#,Unit Testing,Vs Unit Testing Framework,Fluent Assertions,我有两个相同类型的对象需要比较,但是objectA上一个对象属性的值应该等于objectB上另一个名称的属性 鉴于我的目标: class MyObject { public string Alpha {get; set;} public string Beta {get; set;} } var expected = new MyObject {"string1", "string1"}; var actual = new MyObject {"string1", null};

我有两个相同类型的对象需要比较,但是objectA上一个对象属性的值应该等于objectB上另一个名称的属性

鉴于我的目标:

class MyObject
{
    public string Alpha {get; set;}
    public string Beta {get; set;}
}

var expected = new MyObject {"string1", "string1"};
var actual = new MyObject {"string1", null};
我需要验证实际的.Alpha==预期的.Alpha 而实际的.Beta==预期的.Alpha


可以这样做吗?

我想你想要这样的东西:

// VS Unit Testing Framework
Assert.IsTrue(actual.Alpha == expected.Alpha, "the Alpha objects are not equals");
Assert.IsTrue(actual.Beta == expected.Beta, "the Beta objects are not equals");

// Fluent Assertion
actual.Alpha.Should().Be(expected.Alpha);
actual.Beta.Should().Be(expected.Beta);
另外,如果要比较对象列表

// Helper method to compare - VS Unit Testing Framework
private static void CompareIEnumerable<T>(IEnumerable<T> one, IEnumerable<T> two, Func<T, T, bool> comparisonFunction)
{
    var oneArray = one as T[] ?? one.ToArray();
    var twoArray = two as T[] ?? two.ToArray();

    if (oneArray.Length != twoArray.Length)
    {
        Assert.Fail("Collections have not same length");
    }

    for (int i = 0; i < oneArray.Length; i++)
    {
        var isEqual = comparisonFunction(oneArray[i], twoArray[i]);
        Assert.IsTrue(isEqual);
    }
}

public void HowToCall()
{
    // How you need to call the comparer helper:
    CompareIEnumerable(actual, expected, (x, y) => 
        x.Alpha == y.Alpha && 
        x.Beta == y.Beta );
}
//比较-VS单元测试框架的Helper方法
私有静态void compareEnumerable(IEnumerable one、IEnumerable two、Func comparisonFunction)
{
var oneArray=1作为T[]?1.ToArray();
var tworArray=two作为T[]?two.ToArray();
if(oneArray.Length!=twoArray.Length)
{
Assert.Fail(“集合的长度不同”);
}
for(int i=0;i
x、 Alpha==y.Alpha&&
x、 β=y.β);
}

感谢您的回复。这是否也可以应用于集合对象?说我已经预料到了;列出实际情况;实际。应等同于(预期);我认为流畅的断言不支持这种比较。我知道如何在NUnit中使用它,以及如何创建一个方法来在vs单元测试框架中使用它。我将编辑我的awnser并编写方法