如何测试与特定模式/正则表达式匹配的目录路径?
我进行了以下测试,旨在确保生成的文件路径具有特定格式。使用 Nunit 的流畅界面,我该怎么做呢?
我在使用正则表达式时遇到问题。
[Test]
public void AssetControlPath_ShouldHaveFormat_BaseDir_YYYY_MMM_YYYYMMMDD()
{
//Arrange
var baseDir = "C:\\BaseDir";
var fpBuilder = new FilePathBuilder(new DateTime(2010,10,10), baseDir );
//Act
var destinationPath = fpBuilder.AssetControlPath();
//Assert
// destinationPath = C:\BasDir\2010\Oct\20101010
Assert.That(destinationPath, Is.StringMatching(@"C:\\BaseDir\\d{4}\\[a-zA-Z]{3}\\d{8}"));
}
单元测试错误 XXX.FilepathBuilderTests.AssetControlPath_ShouldHaveFormat_BaseDir_YYYY_MMM_YYYYMMMDD: 预期:匹配“C:\\BaseDir\\d{4}\\[a-zA-Z]{3}\\d{8}”的字符串 但是是:“C:\BaseDir\2010\Oct\20101010”
编辑: 我实际上已经将测试切换为使用@ChrisF 的方法。然而这个问题仍然存在。
I have the following test which aims to ensure that file path generated is of a specific format. Using Nunit's fluent interfaces, how can I go about this?
I am having trouble with the regex.
[Test]
public void AssetControlPath_ShouldHaveFormat_BaseDir_YYYY_MMM_YYYYMMMDD()
{
//Arrange
var baseDir = "C:\\BaseDir";
var fpBuilder = new FilePathBuilder(new DateTime(2010,10,10), baseDir );
//Act
var destinationPath = fpBuilder.AssetControlPath();
//Assert
// destinationPath = C:\BasDir\2010\Oct\20101010
Assert.That(destinationPath, Is.StringMatching(@"C:\\BaseDir\\d{4}\\[a-zA-Z]{3}\\d{8}"));
}
The unit test errorXXX.FilepathBuilderTests.AssetControlPath_ShouldHaveFormat_BaseDir_YYYY_MMM_YYYYMMMDD:
Expected: String matching "C:\\BaseDir\\d{4}\\[a-zA-Z]{3}\\d{8}"
But was: "C:\BaseDir\2010\Oct\20101010"
Edit:
I have actually switched the test to use @ChrisF's approach. The question however still stands.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
\ 作为分割字符的
String.Split
,然后检查是否获得了正确的元素数量 (5) 以及每个元素是否为预期值,可能是:以
) 更清楚测试的目的,并且
b) 更易于维护。
A
String.Split
with\
as the split character and then check that you get the right number of elements (5) and that each element is the expected value might be:a) be clearer in what the intent of the test is and
b) easier to maintain.
另外,您遇到
\
转义问题,您需要\\\d{4}
和\\\d{8}
,您想要匹配xxx\20101010
而不是xxx20101010
。以下修复匹配正确:Also you have a problem with
\
escaping, you need\\\d{4}
and\\\d{8}
, you want to matchxxx\20101010
and notxxx20101010
. The following fix matches correct:你有一个额外的右方括号!把它去掉就可以了。
You have an extra closing square bracket!! remove that and it chould be fine.