[英]Unit Testing an implicit operator with NUnit
I'm trying to figure out how to write a unit test around an implicit conversion with NUnit.我试图弄清楚如何围绕 NUnit 的隐式转换编写单元测试。
So far the only way I have been able to do it is to create a static function that takes the type the object will be converted to as a parameter and instantly returns it.到目前为止,我能够做到的唯一方法是创建一个静态函数,该函数将对象将转换为的类型作为参数并立即返回它。
public class Foo
{
public Foo(string name)
{
Name = name;
}
public string Name { get; set; }
}
public class Bar
{
public Bar(string name)
{
Name = name;
}
public string Name { get; set; }
public static implicit operator Foo(Bar bar)
{
return new Foo(bar.Name);
}
}
[Test]
public void BarToFooImplicitConversionTest()
{
var bar = new Bar("FooBar");
var foo = ConvertBarToFooImplicitly(bar);
foo.Name.Should().Be("FooBar");
}
private static Foo ConvertBarToFooImplicitly(Foo foo)
{
return foo;
}
Is there something in NUnit I'm missing or is there a better way to do this? NUnit 中是否有我遗漏的东西,或者有更好的方法来做到这一点?
Just use explicit types instead of var
to trigger implicit operators, eg只需使用显式类型而不是
var
来触发隐式运算符,例如
[Test]
public void Should_be_possible_to_convert_bar_to_foo()
{
//Arrange
const string fooBar = "FooBar";
Bar bar = new Bar(fooBar);
//Act
Foo foo = bar;
//Assert
foo.Name.Should().Be(fooBar);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.