次の方法で共有


Use The Generate Method Stub Smart Tag To Stay In The Zone

Yet Another Zero-Friction TDD Article (YAZFTA):

When writing unit tests in the TDD fashion, it's important to stay in the zone and not get side-tracked by irrelevant issues. You need to avoid what I call mental context switching. Focus on writing the test and postpone all else to later, but make sure that you do it in such a way that you don't have to waste brainpower on remembering what you postponed.

One easy way to do this is by using the Generate method stub smart tag in Visual Studio:

GenerateMethodStubSmartTag

As we all know, this will generate the following implementation in MyClass:

 public void MyMethod()
 {
     throw new NotImplementedException();
 }

but since you already know that, you don't have to go look at it, and you don't even need to commit to memory that you will have to implement the method. When you run the test, it's going to throw a NotImplementedException, and that's all the reminder you need.

So just use the smart tag and move on. Right away, forget about the method you just created. Resist the temptation to go implement it immediately, no matter how simple it might be. Stay focused on writing the test.

You can use the same technique when it comes to evolving SUT API Encapsulation: Sometimes, I know that I'd really like a test helper method that can do something for me, but I have only a vague idea about how to implement it. In such cases, I just write the name of the method, use the smart tag and move on. This helps me stay focused on writing the test, and I can always come back later and implement the helper method at a more convenient time.

Comments

  • Anonymous
    January 04, 2009
    PingBack from http://blogs.msdn.com/ploeh/archive/2008/11/13/zero-friction-tdd.aspx

  • Anonymous
    January 05, 2009
    Resharper does the same. You do use resharper right? I guess anyone writing a zero-friction article should be =)

  • Anonymous
    January 05, 2009
    Hi Ben Thank you for your comment. No, I don't use ReSharper - in part out of habit, in part out of principle. Not that I have anything against ReSharper at all, but one of my principles for the blog (so far) is to stay pretty much within the bounds of what's given by the Microsoft products themselves and see how much I can push the envelope with the tools I have. It forces me to stay sharp, instead of having a sharp tool :) But it's also an old habit of mine not relying on third-party tools. For five years I worked as a consultant for Microsoft Services, and I didn't wish to come to rely on a tool my customers typically didn't have. However, it's a subject I'm regularly considering, and I may decide to take the plunge at some time :)

  • Anonymous
    January 11, 2009
    Zero-Friction TDD post #10: In principle, defining properties (or fields, for that matter) while TDD'ing

  • Anonymous
    August 11, 2012
    Caved in on the R# yet? (I'm using CR but whenever I pair with an R#er, it's hard not to feel like an idiot!)

  • Anonymous
    August 11, 2012
    No, I use neither R# nor CR...