Microsoft Fakes (formerly Moles)

Problem: You are writing unit tests that involve a dependency on a 3rd party concrete class. Unfortunately there are no interfaces, nor are the methods/properties defined as virtual.

Solution: Microsoft Fakes (formerly Moles)

Microsoft Fakes help you isolate the code you are testing by replacing other parts of the application with stubs or shims. These are small pieces of code that are under the control of your tests. By isolating your code for testing, you know that if the test fails, the cause is there and not somewhere else. Stubs and shims also let you test your code even if other parts of your application are not working yet.

Example:

[code lang=”csharp”]

// Code under test:
public int GetTheCurrentYear()
{
return DateTime.Now.Year;
}

[TestClass] public class TestClass1
{
[TestMethod] public void TestCurrentYear()
{
int fixedYear = 2000;

// Shims can be used only in a ShimsContext:
using (ShimsContext.Create())
{
// Arrange:
// Shim DateTime.Now to return a fixed date:
System.Fakes.ShimDateTime.NowGet =
() =>
{ return new DateTime(fixedYear, 1, 1); };

// Instantiate the component under test:
var componentUnderTest = new MyComponent();

// Act:
int year = componentUnderTest.GetTheCurrentYear();

// Assert:
// This will always be true if the component is working:
Assert.AreEqual(fixedYear, year);
}
}
}
[/code]

Idiotic Interview Questions

Interviews questions always seem to be a topic that comes up frequently at a developer peer group I attend and while at work.  In a recent .NET Rocks! podcast, they touched on this topic and brought up the Fizz Buzz Test.  We have all been in interviews where we are given some ridiculous programming question, that is “intended” to show the interviewer your problem solving skills.  Or maybe (I think likely) they ask these questions because they are the stock questions, and everyone asks them.  It’s like asking someone for their strengths and weaknesses.  Do you really think people aren’t telling you what you want to hear.  These stock questions have stock answers.

Back to the ridiculous programming questions.   If someone asked me today in an interview some asinine array sorting question, or better yet a problem that you rarely encounter in the real world, I think my answer would be: Google.

Most of the problems that I encounter in the real world have been already dealt with.  I do not need to come up with my own solution.  I’m not arrogant enough to think that my solution is the best.  Using available resources (ie, Google) to to find the best solution to a problem seems like a better answer.  After all, wasn’t the point of the question to see your problem solving skills?  Why not ask questions related to Patterns, Practices, and Principles?  If the interviewee can discuss SOLID principles, doesn’t the Fizz Buzz Test seem idiotic to ask?