繁体   English   中英

在新的ASP.NET 5类库(包)中将正则表达式与XUnit一起使用

[英]Using regular expressions with XUnit in a new ASP.NET 5 Class Library (package)

我在新的ASP.NET 5类库中使用XUnit并尝试使用Assert.Matches,但不断从intellisense中得到以下错误:
The type 'Regex' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Text.RegularExpressions.... The type 'Regex' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Text.RegularExpressions.... ' The type 'Regex' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Text.RegularExpressions....

我目前仅针对dnx451框架。 知道为什么我会收到此错误吗? 我尝试将System.Text.RegularExpressions添加为project.json中的依赖项,然后将所有代码错误删除。

最大的问题是:这真的是问题还是烦恼? 我问这个问题是因为我不相信这是一个真正的问题,但是让我解释发生了什么以及可以采取什么措施进行故障排除。

首先,来自智能感知的建议

The type 'Regex' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Text.RegularExpressions....'

这是因为没有Intellisense for XUnit。 如果我在这里,这不是一个真正的问题,而是一个刺激点。 Visual Studio Intellisense会看到Regex对象,在System.Text.RegularExpressions中识别出一个对象,并要求您引用它。 然后,当您进行编译时,它将尝试使用System.Text.RegularExpresssions中的对象而不是XUnit。 它很可能由于没有适当签名的方法而崩溃,这不足为奇。

删除对SYstem.Text的引用。 它仍然炸毁吗? 它运作正常吗? 介于两者之间。

故障排除:

首先,确保引用了其中包含其Regex对象(而不是System.Text.RegularExpressions regex对象)的正确XUnit库。 然后,确保顶部具有正确的using语句以使用它(或完全合格)。

接下来,如果Intellisense仍然触发并惹恼您,请考虑使用XUnit.Contrib。 不知道它是否是最新的,因为我已经有一段时间没有接触XUnit了,但是它确实包含了Resharper位: https : //github.com/xunit/resharper-xunit

如果这是死胡同,请查看是否在任何地方都有XUnit Intellisense。 如果没有,您可以创建自己的自定义智能感知: http : //www.wyndhamhudson.com/adding-custom-intellisense-to-visual-studio-2010/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM