简体   繁体   English

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

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

I am using XUnit in a new ASP.NET 5 class library and trying to use Assert.Matches but keep getting the following error from intellisense: 我在新的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.... ' The type 'Regex' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Text.RegularExpressions....

I am currently only targeting the dnx451 framework. 我目前仅针对dnx451框架。 Any idea why I am getting this error? 知道为什么我会收到此错误吗? I tried adding System.Text.RegularExpressions as a dependency in project.json and then all of my code errors out. 我尝试将System.Text.RegularExpressions添加为project.json中的依赖项,然后将所有代码错误删除。

The big question is this: Is it really a problem or an annoyance? 最大的问题是:这真的是问题还是烦恼? I ask this as I am not convinced this is a real problem, but let me explain what is happening and what you can do to troubleshoot. 我问这个问题是因为我不相信这是一个真正的问题,但是让我解释发生了什么以及可以采取什么措施进行故障排除。

First, the suggestion from intellisense 首先,来自智能感知的建议

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

This is due to not having Intellisense for XUnit. 这是因为没有Intellisense for XUnit。 It is not a real problem, if I am right here, but an irritation point. 如果我在这里,这不是一个真正的问题,而是一个刺激点。 Visual Studio Intellisense sees the Regex object, recognizes there is one in System.Text.RegularExpressions, and asks you to reference it. Visual Studio Intellisense会看到Regex对象,在System.Text.RegularExpressions中识别出一个对象,并要求您引用它。 Then when you compile, it tries to use the object from System.Text.RegularExpresssions instead of XUnit. 然后,当您进行编译时,它将尝试使用System.Text.RegularExpresssions中的对象而不是XUnit。 It blows up, most likely, due to not having a method with the proper signature, which should not be a surprise. 它很可能由于没有适当签名的方法而崩溃,这不足为奇。

Remove the reference to SYstem.Text. 删除对SYstem.Text的引用。 Does it still blow up? 它仍然炸毁吗? Does it work properly? 它运作正常吗? Something in between. 介于两者之间。

Troubleshooting: 故障排除:

First, make sure you are referencing the proper XUnit library that has its Regex object (not the System.Text.RegularExpressions regex object) in it. 首先,确保引用了其中包含其Regex对象(而不是System.Text.RegularExpressions regex对象)的正确XUnit库。 Then make sure you have a proper using statement at the top to use it (or fully qualify). 然后,确保顶部具有正确的using语句以使用它(或完全合格)。

Next, if the Intellisense still fires off and annoys you, consider XUnit.Contrib. 接下来,如果Intellisense仍然触发并惹恼您,请考虑使用XUnit.Contrib。 Not sure if it is up to date, as I have not touched XUnit in awhile, but it did have Resharper bits in it: https://github.com/xunit/resharper-xunit 不知道它是否是最新的,因为我已经有一段时间没有接触XUnit了,但是它确实包含了Resharper位: https : //github.com/xunit/resharper-xunit

If this is a dead end, see if there is XUnit Intellisense anywhere. 如果这是死胡同,请查看是否在任何地方都有XUnit Intellisense。 If not, you can create your own custom intellisense: http://www.wyndhamhudson.com/adding-custom-intellisense-to-visual-studio-2010/ 如果没有,您可以创建自己的自定义智能感知: 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