Tuesday, March 25, 2008

My MCTS Certification

Today I have appeared at the MCTS Exam # 70-528 (Web Based Client Development) and successfully passed the exam. I also appeared at another exam # 70-536 (Microsoft .Net Framework) and successfully passed in that exam too.

Although, I haven't got the MCP website access information yet, I can now possibly be called a 'Microsoft Certified Technology Specialist'!

Thursday, March 20, 2008

A solution to the problem with creating mocks for interfaces with Generic Methods with NMock

I was so happy with NMock to see how it can dynamically generate mocks of interfaces and also gives me a fluent interface to write expectations and everything it does to help me in unit testing!

However, I could not generate dynamic mocks for interfaces with Generic methods and it kept showing me the 'TypeLoadException' on and on. My interface looks like the following -

interface IObjectFactory
{
T GetObject(string id);
}

As I failed to create a mock implementation of this interface using NMock, I just wrote a mock implementation myself, which looks like the following

class MockObjectFactory: IObjectFactory
{
public Type RequestedObjectType;
public string RequestedObjectId;
public T GetObject(string id)
{
RequestedObjectType = typeof(T);
RequestedObjectId = id;
return new Mockery().NewMock();
}
}


And in my test code, just injected the above mock implementation of the IObjectFactory where needed. So, although NMock cannot handle mocking of this type of an interface now, you can actually create a mock implementation and use NMock inside the implementation to help you.

Tuesday, March 18, 2008

Verify Correct DateTime data in method call using NMock

Recently I was writing unit test for a method of the following signature

interface ICache
{
void
Insert(string key, object value, DateTime absoluteExpiration, TimeSpan slidingExpiration);
}

and I wanted to verify the following call -

Insert("key", "value", DateTime.Now.AddMinutes(30), TimeSpan.Zero);


However, you readily see the problem in verifying with the above call for the presence of
DateTime.Now.AddMinutes(30) in the argument. So, a test method like the following won't work for obvious reason.
ICache cache = _mocks.NewMock<ICache>();
Expect.Once.On(cache).Method("Insert").With("key", "value", DateTime.Now.AddMinutes(30), TimeSpan.Zero);


The reason that the above sample code wont work is, my test code called DateTime.Now.AddMinutes(30) before the production code actually did it. So, it is highly probable that the two version of DateTime.Now.AddMinutes(30) in the test code and production code are not same.

To find a work around to this problem, I changed my assumption a little. I set my assumption that the value for the '
absoluteExpiration' parameter must lie in between 29 and 31 minutes from now. So, I modified my test method like the following call-

Expect.Once.On(cache).Method("Insert").With("key", "value", Is.NotNull & Is.AtLeast(DateTime.Now.AddMinutes(29)) & Is.AtMost(DateTime.Now.AddMinutes(31)), TimeSpan.Zero);

This advanced level of NMock usage may show you a way in similar needs. For more insight to this solution, you may wish to visit http://nmock.org/advanced.html



Sunday, March 16, 2008

Mock Internal Interface with NMock2 - Use InternalsVisibleToAttribute

Yesterday I badly needed to create mocks for my internal interfaces. I am using NMock2 as a Mocking framework and found it really difficult to produce Mocks for internal interfaces. After some 'google time' and investigation I found that you need the following two lines in your AssemblyInfo.cs to achieve this purpose-

[assembly: InternalsVisibleTo("Mocks")]
[assembly: InternalsVisibleTo("MockObjects")]

Where are the two assemblies? I found that these two assemblies are dynamically created by NMock2 for mocking your interfaces.

To find out the truth yourself, just put a breakpoint and watch the value of the following line-

AppDomain.CurrentDomain.GetAssemblies()

You should see the two assemblies in the list of the assemblies in the current application domain.

I found it interesting. I wish someone of you might find it useful too!

Note: You may still need to add InternalsVisibleTo attribute for your test assembly with the above two declarations.

Saturday, March 15, 2008

Unity - A Dependency Injection framework from Microsoft Patterns and Practices

I have been using Spring.Net for implementing Dependency Injection in my .Net projects. Spring is already one of the most known providers of DI in the Java world and a port to .Net actually eased the lives of people like me who are willing to develop unit-testable software.

However, with all the happiness enjoyed through Spring.Net, I guess its time to take a look into Unity. I have only started using this and I found it interesting in the sense that its more of .Net flavored with a few attributes and fluent interfaces.

I hope to post a few concrete examples on Unity implementation soon. However, you may wish to take a look at http://www.codeplex.com/unity in the meanwhile and let me know your comments on this new application block by Microsoft Patterns and Practices group.

Tuesday, March 11, 2008

Testing Internal Class in .Net C#

I have been to a situation where only a small number of classes had been declared as public classes in an assembly. And most of the classes are declared as internal, because I wanted to expose only the public classes to the consumers of my assembly.

However, doing so creates another challenge in testing the internal classes. As we most commonly want our test codes to reside on a separate assembly so that we can leave that assembly out when we are releasing the end product. And the internal access control would makes it difficult to write unit tests for these classes.

One workaround to this problem is to keep the test classes in the same assembly so that the test class has access to the internal class. However, to leave out these test classes from release configurations, we need to apply a conditional compilation for these files.

However, this is a pessimistic or short cut solution to the real problem. We actually want to test an internal class from another assembly (the test assembly) and don't want to go through reflection or other less intuitive paths.

I just found that, adding the following assembly attribute in the AssemblyInfo.cs may do the trick.

[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("TestAssemblyName")]


Adding this attribute will make the assembly's internals visible to the assembly thats specified in the argument.

So, once this attribute is added to an assembly, the test assembly treats the internal classes just as other public ones and need not worry about access protection problems.

Saturday, March 08, 2008

What does a Scrum Master really do?

Hmm! I am now a CSM (Certified Scrum Master) after two days of training with CST Pette Deemer.

You want to congratulate me! Lets congratulate me first-
You: Congratulations! It's really a great news! How does it feel to be a CSM?
Me: Greaaaaaaaat! You know, its so cool that my profile gets a CSM on it!
You: Oh Sohan! I am so sure that you will contribute more at the company?
Me: Yeah, I surely will. I just need a product owner and a team with me to contribute!

(Now that I am so good with the certificate let me be your teacher on 'Scrum Master'ing)

Well, I have been following SCRUM for almost two years in my team and we had a lot of questions in our minds regarding the process. Some of the questions are answered and some are not. But to be very positive, the outcome of the training is good. It clears the philosophy behind SCRUM and also creates a clear vision on why one should follow SCRUM. I will be going on a question answer mode that closely resembles my learning at the training.

Q: Is scrum master a part of the daily scrum meeting?
A: Yes, he is involved, but stays outside the team's SCRUM cycle and takes note whenever someone points out to anything that is a hurdle before the team in meeting sprint commitment.

Q: So, is Scrum Master is a lead role?
A: No. Its rather a helper role to the team.

Q: Is it possible that Scrum Master is not co-located with the team?
A: Generally its bad. It seems unlikely that she can remove blocks from heaven (the distant is always a heaven!).

Q: What are the core responsibilities of the Scrum Master?
A:
Help team to follow SCRUM.
Do anything to remove blocks reported by the team.
Do nothing to create any block for the team.
Update the Burn Down charts, the 'Not Started, Started, Completed' board and other progress indicator 'things'.
Facilitate the team in meetings - avoid unnecessary time loss meetings, manage meeting place and things like laptop, projector, mic and sound systems... all that it takes for the team to have a potentially meaningful and quick meeting.

Q: Is Scrum Master part of the team?
A: Well, it depends. If the team is small and doesn't need/cannot afford a full time scrum master, someone from the team may take on this responsibility. But, a dedicated one is most likely to be effective.

Q: What should be the bullet points of the Scrum Master's characteristics?
A: Helping, self-motivated and feels comfortable to help people on any sort of problem.

Q: Enough is Enough. What else do you know about Scrum Master?
A: Hmm... Scrum Master is not executed if the team does not meet their commitment (unless its because of the fact that the Scrum Master wasn't able to remove blocks). Scrum Master always guards the team from product owner's pressure and change requests in the middle of sprints.

I could have a longer list of Q & A that I have actually learned at the training. But I am not going to do that partly because, I have only a few readers (namely I, me, myself and Sohan) in my blog and partly because I do not see comments on my posts too often.

So, if you have read this and really think you want to know more, energize me with your comments and questions :-). I will be really glad to share my learnings with you.