-->

How to debug in Visual Studio with NSpec

2020-07-20 04:34发布

问题:

how do I debug in visual studio with NSpec? I've resharper installed I need to step into my test code.

回答1:

At least in Visual Studio 2013, the NSpec Test Adapter (by {o} Software) seems to do the trick. Find it in the Extensions gallery. Then just right-click on the test in the Test Explorer and hit Debug.



回答2:

Another good option is to just type

  System.Diagnostics.Debugger.Launch()

in the test you want to debug. You'll get a Visual Studio prompt to debug the test.

I would also recommend taking a look at specwatchr.



回答3:

I use a simple trick that let's me debug NSpec with resharper out of the box. The idea is to have your own version of "nspec" class instead of DebuggerShim.cs (somewhere in your test project):

[TestFixture]
public abstract class nspec: global::NSpec.nspec
{
    [Test]
    public void debug()
    {
        var currentSpec = this.GetType();
        var finder = new SpecFinder(new[] {currentSpec});
        var builder = new ContextBuilder(finder, new Tags().Parse(currentSpec.Name), new DefaultConventions());
        var runner = new ContextRunner(builder, new ConsoleFormatter(), false);
        var results = runner.Run(builder.Contexts().Build());

        //assert that there aren't any failures
        results.Failures().Count().should_be(0);
    }
}

After that you will get Resharper's test icon next to your spec:

For more details see this article: Debugging NSpec with VisualStudio/Resharper – simple trick



回答4:

Resharper supports NUnit and MSTest runners only (AFAIR). Open the Resharper "Unit Test Explorer" window and see if it lists your NSpec tests there..

If not, you need to fallback to specifying an external program for debugging (The NSpec runner exe) - See this question on how to configure this in Visual Studio.