microsoft / UnitTestBoilerplateGenerator

An extension for Visual Studio that generates a unit test boilerplate from a given class, setting up mocks for all dependencies. Supports NUnit, Visual Studio Test, Moq and SimpleStubs.
MIT License
158 stars 51 forks source link

Issue: Namespace generated incorrect in solution with multiple projects #8

Closed BlacKCaT27 closed 7 years ago

BlacKCaT27 commented 7 years ago

Installed product versions

Description

I have a solution with 5 different projects, and when I generate the boilerplate file and specify the test project, the namespace doesn't contain the test project name, starting with a "." instead, which results in compiler errors.

Could this be related to my project being ASP.NET Core? Has this library been tested there?

Steps to recreate

  1. Create ASP.NET Core solution with a test project aside from the src projects.
  2. Generate a test file using the tool for a non-test-project class.
  3. Specify the Test project.

Current behavior

namespace is missing the test project name, resulting in a compiler error as it starts with "."

Expected behavior

namespace contains the project name followed by the correct path

RandomEngy commented 7 years ago

I just tried with a class in an ASP.NET Core project going to a separate test project and it works for me. Can you supply a small repro project so I can figure out what's going on?

BlacKCaT27 commented 7 years ago

Did you add an additional non-test project to the solution? That's where I'm seeing the problem.

Here's a sample repro project: https://github.com/BlacKCaT27/samplebugunittestboilerplate

You can see when I try to generate boilerplate for ClassLibrary1.Class1, it successfully creates the file in the ClassLibraryTests file (named Class1Tests), but the namespace is missing the project name so compilation fails

RandomEngy commented 7 years ago

Thanks for the project. I actually got a different error when opening with VS 2015: it can't even find the source document to start analyzing it with Roslyn. Though it won't hit any breakpoints and I can't open the project in VS 2017 even though I have the ASP.NET Core workload enabled.

I think I'm going to leave this one as .xproj is going away and I suspect that's the source of my issues. Unless you want to submit a pull request.

BlacKCaT27 commented 7 years ago

You know, I'm sorry, that error might be on my end. That project might not have been set up correctly as I threw it together while in the middle of something else.

Just to verify real quick, could you add an additional project to the test solution you originally put together, and generate a test class out of one of the classes in that? I'll need to test it out more myself when I get into the office, but I suspect the issue could be with classes outside the startup package.

It could be that this is related to project.json vs. xproj, in which case, when they revert back to xproj you're right this might all just fix itself, but I guess we'll see.

On Tue, Jan 31, 2017 at 3:11 AM, RandomEngy notifications@github.com wrote:

Thanks for the project. I actually got a different error when opening with VS 2015: it can't even find the source document to start analyzing it with Roslyn. Though it won't hit any breakpoints and I can't open the project in VS 2017 even though I have the ASP.NET Core workload enabled.

I think I'm going to leave this one as .xproj is going away http://ivanz.com/2016/05/17/farewell-project-json-hello-msbuild-and-csproj and I suspect that's the source of my issues. Unless you want to submit a pull request.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Microsoft/UnitTestBoilerplateGenerator/issues/8#issuecomment-276300118, or mute the thread https://github.com/notifications/unsubscribe-auth/AE_54YJ6Ku5PxnKIfPNwtLsr7EMXJTAfks5rXuyWgaJpZM4LtrNv .

RandomEngy commented 7 years ago

Yeah I think the issue must have been with this line:

string defaultNamespace = this.SelectedProject.Project.Properties.Item("DefaultNamespace").Value as string;

It couldn't find the DefaultNamespace in the source project.

BlacKCaT27 commented 7 years ago

Sorry, is that the source of the bug in the repo? Or the issue you were having with the test project I gave you?

I just tried a couple more test solutions on my machine and even without multiple projects (even a test project), the plugin fails to write the namespace every time. I've verified the default namespace value is set in my project properties.

RandomEngy commented 7 years ago

That's the source of the bug in the repo. VS must not know or must not be communicating that to my extension. I've been having too much trouble to repro it; you can submit a pull request to fix if you'd like.

BlacKCaT27 commented 7 years ago

Forked the repo to try and fix but unfortunately it appears the repo's solution file was made with VS2017, which I don't have access to here at the office. I'll see if there's a community edition or trial I can use at home to maybe work on this tonight.

Barring me actually getting that working, though, a simple stop-gap solution could be that in the event "defaultNamespace" is null or empty after assignment, use the solution name as a fallback default. Then maybe alert the user via a warning log or popup calling out they may want/need to update the namespace value.

At least this way the plugin is still generating compiling code.

BlacKCaT27 commented 7 years ago

I spent a few hours trying to get the dev environment up and running and find a fix. Got close, but couldn't get debugging the extension to work.

I'm not entirely sure this is even the right section of code causing the problem, however. I tried hardcoding the defaultNamespace variable just to see if it would give me a namespace, and it didn't.

I think I know why you're having trouble reproducing what I'm seeing, actually. I just checked, and provided the selected class has a namespace (i.e. not in the global namespace), if I use Visual Studio 2017, the generator works perfectly fine in a .net core project.

It's only in 2015 where it fails. Could you try reproducing the problem on your end in 2015? They have a community edition whose license is free for OSS devs.

RandomEngy commented 7 years ago

I can reproduce the problem in 2015 but as you found I can't debug in it. Breakpoints don't get hit.

BlacKCaT27 commented 7 years ago

Ah, unfortunate. was hoping you'd know more about how to resolve that since I've never worked in visual studio extensions before.

So no hope of this getting a fix then for 2015? :(

On Wed, Feb 1, 2017 at 12:35 AM, RandomEngy notifications@github.com wrote:

I can reproduce the problem in 2015 but as you found I can't debug in it. Breakpoints don't get hit.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Microsoft/UnitTestBoilerplateGenerator/issues/8#issuecomment-276576640, or mute the thread https://github.com/notifications/unsubscribe-auth/AE_54ZMnGTqrOcpCyndmgwiss_srQnSEks5rYBmHgaJpZM4LtrNv .

RandomEngy commented 7 years ago

Well I wouldn't say there is no hope, but I'm not going to work on it.