在 xUnit 中正确使用 Theory
Correct usage of Theory in xUnit
我一直在阅读有关 xunit 的文章,但对如何使用 Theory 有点困惑。
假设我有这个测试:
[Theory]
[InlineData("")]
[InlineData("anything")]
public void AddItem_NoName_ThrowsNoNameException(string name)
{
// Arrange.
ItemService itemService = new ItemService();
Item item = new Item();
item.Name = name;
// Act.
Exception ex = Record.Exception(() => itemService.AddItem(item));
// Assert.
Assert.NotNull(ex);
Assert.IsType<NoNameException>(ex);
}
以及被测方法:
public void AddItem(Item item)
{
if (item.Name == "")
throw new NoNameException();
_DAL.AddItem(item);
}
因此这会创建两个单元测试:一个传递空字符串 ("") 的参数,一个传递单词 "anything"。所以第一个(空字符串)通过了,因为我们有代码检查 item.Name 是否为空字符串,抛出异常。然而,第二个失败了,因为单元测试中的 Assert.NotNull(ex) 将是错误的。但是,如果您查看 AddItem 方法,该方法的编码是正确的,所以我想看到两个测试都通过了。
也许我通过单元测试实现的方式是错误的,但在这种情况下我想要发生或我认为应该发生的是两个测试都应该通过,因为被测方法是正确的(基于规则).我该怎么做?
更新:
好的,我已经通过执行下面的代码设法使这两项都通过了。但是,我认为要有条件断言是不正确的。仍然需要帮助。
[Theory]
[InlineData("", false)]
[InlineData("anything", true)]
public void AddItem_NoName_ThrowsNoNameException(string name, isValid)
{
// Arrange.
ItemService itemService = new ItemService();
Item item = new Item();
item.Name = name;
// Act.
Exception ex = Record.Exception(() => itemService.AddItem(item));
// Assert.
if (!isValid)
{
Assert.NotNull(ex);
Assert.IsType<NoNameException>(ex);
}
else
{
Assert.Null(ex);
}
}
这是两个不同的测试用例,应该编写单独的测试。 Theory
测试代码相同时应使用
[Theory]
[InlineData("")]
[InlineData(null)] // will fail based on your impl, just used for example
public void AddItem_NoName_ThrowsNoNameException(string name)
{
// Arrange
ItemService itemService = new ItemService();
Item item = new Item();
item.Name = name;
// Act & Assert
Assert.Throws<NoNameException>(() => itemService.AddItem(item));
}
[Fact]
public void AddItem_WithName_DoesXYZ()
{
// Arrange
ItemService itemService = new ItemService();
Item item = new Item();
item.Name = "anything";
// Act
itemService.AddItem(item);
// Assert
# verify item was added to db
}
我一直在阅读有关 xunit 的文章,但对如何使用 Theory 有点困惑。
假设我有这个测试:
[Theory]
[InlineData("")]
[InlineData("anything")]
public void AddItem_NoName_ThrowsNoNameException(string name)
{
// Arrange.
ItemService itemService = new ItemService();
Item item = new Item();
item.Name = name;
// Act.
Exception ex = Record.Exception(() => itemService.AddItem(item));
// Assert.
Assert.NotNull(ex);
Assert.IsType<NoNameException>(ex);
}
以及被测方法:
public void AddItem(Item item)
{
if (item.Name == "")
throw new NoNameException();
_DAL.AddItem(item);
}
因此这会创建两个单元测试:一个传递空字符串 ("") 的参数,一个传递单词 "anything"。所以第一个(空字符串)通过了,因为我们有代码检查 item.Name 是否为空字符串,抛出异常。然而,第二个失败了,因为单元测试中的 Assert.NotNull(ex) 将是错误的。但是,如果您查看 AddItem 方法,该方法的编码是正确的,所以我想看到两个测试都通过了。
也许我通过单元测试实现的方式是错误的,但在这种情况下我想要发生或我认为应该发生的是两个测试都应该通过,因为被测方法是正确的(基于规则).我该怎么做?
更新:
好的,我已经通过执行下面的代码设法使这两项都通过了。但是,我认为要有条件断言是不正确的。仍然需要帮助。
[Theory]
[InlineData("", false)]
[InlineData("anything", true)]
public void AddItem_NoName_ThrowsNoNameException(string name, isValid)
{
// Arrange.
ItemService itemService = new ItemService();
Item item = new Item();
item.Name = name;
// Act.
Exception ex = Record.Exception(() => itemService.AddItem(item));
// Assert.
if (!isValid)
{
Assert.NotNull(ex);
Assert.IsType<NoNameException>(ex);
}
else
{
Assert.Null(ex);
}
}
这是两个不同的测试用例,应该编写单独的测试。 Theory
测试代码相同时应使用
[Theory]
[InlineData("")]
[InlineData(null)] // will fail based on your impl, just used for example
public void AddItem_NoName_ThrowsNoNameException(string name)
{
// Arrange
ItemService itemService = new ItemService();
Item item = new Item();
item.Name = name;
// Act & Assert
Assert.Throws<NoNameException>(() => itemService.AddItem(item));
}
[Fact]
public void AddItem_WithName_DoesXYZ()
{
// Arrange
ItemService itemService = new ItemService();
Item item = new Item();
item.Name = "anything";
// Act
itemService.AddItem(item);
// Assert
# verify item was added to db
}