简体   繁体   中英

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:
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. 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.

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. 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. Then when you compile, it tries to use the object from System.Text.RegularExpresssions instead of 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. 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. Then make sure you have a proper using statement at the top to use it (or fully qualify).

Next, if the Intellisense still fires off and annoys you, consider 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

If this is a dead end, see if there is XUnit Intellisense anywhere. If not, you can create your own custom intellisense: http://www.wyndhamhudson.com/adding-custom-intellisense-to-visual-studio-2010/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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