Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
482 views
in Technique[技术] by (71.8m points)

c# - xUnit的Assert.Equal方法是检查同一引用还是在寻找两个不同的对象实例?(Is Assert.Equal method from xUnit checking the same reference or looking for two different object instances?)

I started to use xUnit infrastructure.

(我开始使用xUnit基础结构。)

So I tried to use void Assert.Equal(string expected, string actual) method.

(所以我尝试使用void Assert.Equal(string expected, string actual)方法。)

[Fact]
public void CanChangeProductName()
{
    // Arrange
    var p = new Product()
    {
        Name = "Name",
        Price = 180M
    };
    var new_name = "NewName";
    // Act
    p.Name = (string)new_name.Clone(); // ?
    // Assert
    Assert.Equal(new_name, p.Name);
}

And wondered, should I use string.Clone() method to create truly new object instead of comparison the same link to single object?

(想知道,是否应该使用string.Clone()方法创建真正的新对象,而不是将同一链接与单个对象进行比较?)

  ask by Павел Бобров translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

No need to clone to verify the expected behavior

(无需克隆即可验证预期的行为)

[Fact]
public void CanChangeProductName() {
    // Arrange
    var expected = "NewName";
    var subject = new Product() {
        Name = "Name",
        Price = 180M
    };

    // Act
    subject.Name = expected;
    var actual = subject.Name;

    // Assert
    Assert.Equal(expected, actual);
}

Now unless there is something special happening within the subject under test that was not shown in the original question, I am not seeing much value in testing that property.

(现在,除非被测对象内部发生特殊情况,而原始问题未显示该特殊情况,否则在测试该属性时我不会看到太多价值。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...