James Penick

I have a new test project that includes a reference to the global System.dll as well as another private System.dll that I'll call Foo.System.dll. My problem is that the autogenerated code in VSCodeGenAccessors.cs won't compile because of a namespace collision. For instance, when I try to create private accessors on one of my methods, I get some code that looks like this:

[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TestTools.UnitTestGeneration", "1.0.0.0")]

internal class Foo_FooAccessor : BaseAccessor { . . . }

The compile error says that the type or namespace 'CodeDom' does not exist in the namespace 'Foo.System' (are you missing an assembly reference )

I can temporarily fix the problem by modifying the autogenerated code such that I prepend global:: to the System.CodeDom.Compiler line, but I was wondering if there is a better way to work around this issue Is there any way to force visual studio to seach in the Microsoft System.dll before searching in my private Foo.System.dll Or can I configure visual studio to somehow alway prepend 'global::' to the autogenerated code



Re: Visual Studio Team System - Testing Handling namespace collisions

Koundinya Veluri

Hi James,

I'm assuming the namespace containing the class you are testing is Foo.System, and that the default namespace of your test project is Foo (this is one way to produce the scenario you described). In this situation, in VSCodeGenAccessors.cs, when the compiler searches for System, it finds your private System namespace under Foo, so it stops there and uses your namespace. If it does not find it, only then it will search the global namespace, so there is no way to force it to search the global namespace first, without prepending global::. As for having Visual Studio automatically prepend global:: to all references of System in the autogenerated code, I'm afraid there is no way to do that.

You can manually prepend global:: as you are currently doing, but you would have to do that every time a new private accessor is generated. I would suggest that you change the namespace of your test project in the project properties and in the VSCodeGenAccessors.cs file. This way, any reference to System in the autogenerated code will directly search the global namespace. Typically, it is a good idea to keep the test project in a separate namespace from the project you are testing.

Kount





Re: Visual Studio Team System - Testing Handling namespace collisions

James Penick

Koundinya Veluri wrote:

I'm assuming the namespace containing the class you are testing is Foo.System, and that the default namespace of your test project is Foo (this is one way to produce the scenario you described).

Sorry -- I should have been more specific. No, the namespace of the class that I am testing is not Foo.System. It turns out that my namespace is something entirely different (Foo.Bar). Furthermore, my test project is in the Foo.Bar.Test namespace, so I believe I've already satisfied the suggestion to keep the test project in a separate namespace from the project I am testing. Unfortunately, the class that I am testing has a reference to objects in the Foo.System namespace, so I've included a reference to Foo.System.dll by going into Project -> Add Reference (I've included this reference in both my main project and my test project).

As you've already indicated, there is no way to make Visual Studio prepend global:: to all System references. Is there any way to tell Visual Studio to change the search order so that it will, by default, look in Foo.System only after looking in the global System dll





Re: Visual Studio Team System - Testing Handling namespace collisions

Koundinya Veluri

I see, still though, the search for System would go up the namespace chain starting from Foo.Bar.Test, then Foo.Bar, then Foo, where it stops since it finds Foo.System. There is no way of changing this behavior. If you put your tests in FooTests.Bar instead of Foo.Bar.Test, then it would solve your problem, although I don't know how feasible this is for you.

Kount





Re: Visual Studio Team System - Testing Handling namespace collisions

James Penick

Thanks Kount -- that is a reasonable workaround. I didn't realize that the compiler searches up the namespace chain like you mentioned. I tried this out and it fixes the problem.