下面是我的测试的断言我如何将所有断言组合在一行代码中
public void ThenICanSeeTheFunctionlitiesForNONtransitionUserAsClnician()
{
Assert.IsTrue(ObjectRepository.phPage.GetMenuList().Contains("Show menu"));
Assert.IsTrue(ObjectRepository.phPage.GetMenuList().Contains("Patient Summary"));
Assert.IsTrue(ObjectRepository.phPage.GetMenuList().Contains("Patient Encounter"));
}
假设
ObjectRepository.phPage.GetMenuList()
返回IEnumerable<string>
- 您可以使用MSTest断言
首先,我们需要创建一个项目集合,我们希望在"菜单列表"中有这些项目,以及我们实际拥有的
var expectedItems = new List<string> { "Show menu", "Patient Summary", "Patient Encounter" };
var actualItems = ObjectRepository.phPage.GetMenuList();
现在,根据您的需要,您有两个选项:
1.您想检查"菜单列表"是否包含这3个项目(但并非仅限于这些(
CollectionAssert.IsSubsetOf(expectedItems, actualItems);
2.您想检查"菜单列表"是否只包含这3个项目(没有其他项目(
CollectionAssert.AreEquivalent(expectedItems, actualItems);
如果你真的只想要一个断言,从技术上讲,你可以做这样的事情:
public void ThenICanSeeTheFunctionlitiesForNONtransitionUserAsClnician()
{
var menuItems = ObjectRepository.phPage.GetMenuList();
Assert.IsTrue(menuItems.Contains("Show menu") && menuItems.Contains("Patient Summary") && menuItems.Contains("Patient Encounter"));
}
但我相信,当测试失败时,你会看不清到底缺少了什么菜单项。你现在已经拥有的方式是有意义的。
如果您希望断言在其中一个失败时继续运行,则可以使用Specflow多个断言:
public void ThenICanSeeTheFunctionlitiesForNONtransitionUserAsClnician()
{
Assert.Multiple(() =>
{
Assert.IsTrue(ObjectRepository.phPage.GetMenuList().Contains("Show menu"));
Assert.IsTrue(ObjectRepository.phPage.GetMenuList().Contains("Patient Summary"));
Assert.IsTrue(ObjectRepository.phPage.GetMenuList().Contains("Patient Encounter"));
});
}
如果你只想在测试方法中具有可读性,你可以引入一个私有方法,将你的所有断言都保存在例如中
public void ThenICanSeeTheFunctionlitiesForNONtransitionUserAsClnician()
{
AssertMenuListItems(ObjectRepository.phPage.GetMenuList());
}
private void AssertMenuListItems(TypeOfGetMenuList items)
{
Assert.IsTrue(items.Contains("Show menu"));
Assert.IsTrue(items.Contains("Patient Summary"));
Assert.IsTrue(items.Contains("Patient Encounter"));
}