VS2012 Breakpoints are not getting hit

2019-01-21 22:13发布

I have a class that looks like this:

public class MyService
{
    private MyService(){}
    public static string GetStuff()
    {
        var stuffDid = new MyService();
        return stuffDid.DoStuff();
    }
    private string DoStuff()
    {
        //do stuff
    }
    //other private helpers

}

Obviously I left a lot out, but thats the general shell.

Now, I have a unit test:

[Test]
public void MyTest()
{

    var results = MyService.GetStuff();
}

I set breakpoints on my unit test, and I can see that results has data. However, I set breakpoints literally all over MyService and nothing gets hit unless I put them on a curly brace. Which I can't understand since results has data, my return statements in MyService should be getting hit, right?

Am I missing something? Did I completely forgot the most basic rules of something? How come nothing in MyService gets hit? And if I manually step into it with F11, it just hops around and doesnt even go thru every line like I would expect. Also when I step thru manually I tend to hit certain code after I should have hit it originally. And any switch statements seem to default to whatever the first option is, even tho the value being switched should CLEARLY enter a different case.

I've even tried making MyService constructor public and taking away all static methods, and it still doesnt work.

Edit: My Tests and 'Core' code are in the same solution, but different projects(Test and Core, respectively). Other tests don't have an issue hitting break points in Core, only this on particular test(the only test that is testing MyService.

Edit 2:

I've deleted my PDB files and cleaned solution. Still nothing.

24条回答
劳资没心,怎么记你
2楼-- · 2019-01-21 22:25

First try rebuilding your project by right mouse click the project > Rebuild If that doesn't work, try a clean of the project (right mouse click on the project > clean)

If that didn't work check this:

Right mouse click your project
select [Properties]
select the [Build] tab
make sure [Define DEBUG constant] and [Define TRACE constant] are checked
Click the [Advanced] button at the bottom of the Build tabpage
Make sure that [Debug Info:] is set to [full]
Click [OK] and rebuild the project ;-)

Hope that works for you! (step 6 generates the .pdb files, these are the debugging symbols)

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-01-21 22:26

If it is in release mode switch it to debug mode.

查看更多
萌系小妹纸
4楼-- · 2019-01-21 22:28

I had this happen in a 1 project out of 25 which were all in the same solution. The other projects honored breakpoints but this 1 didn't. I removed the project from the solution (delete, not unload) which broke all references to it and then added it back to the solution and that worked!

If that doesn't work you may want to recreate the problem project from scratch and add that new project to the solution.

The best explanation I have for why this worked apart from pure luck is that we migrated projects from one version of VS to another many, many times over the years and maybe one of those migrations caused this problem.

查看更多
乱世女痞
5楼-- · 2019-01-21 22:29

Silly me the Test Project wasn't set to be built:

enter image description here

查看更多
叛逆
6楼-- · 2019-01-21 22:33

I have a very specific scenario that resulted in the apparent issue of "Breakpoint not being hit".

Since no other answers here mentioned it, I will add mine in the chance that it will help someone who had the same issue.

The solution, in my case, was silly, and with as much LINQ as I use I should have figured this out sooner. When running a method that returns an IEnumerable, where the return statements contained within are actually yield return statements, then that method will not be executed when you call it.

It will actually be executed when you call another method from that IEnumerable object, such as ToList() or Count(). Only then will the method be executed and the breakpoint reached.

查看更多
ゆ 、 Hurt°
7楼-- · 2019-01-21 22:35

You need to make DoStuff static.

private static string DoStuff()
{
    //do stuff
}
查看更多
登录 后发表回答