TypeMock Isolator is a mocking framework for .Net. It uses the .Net profiling API to do its work. This makes it possible for TypeMock to mock parts of your code that other frameworks cannot. TypeMock makes it easy to write unit tests. TypeMock provides three ways to fake dependencies:

  • Relective Mocks. Reflective mocks uses reflection and a string based API,
  • Natural Mocks. It is a Record-Replay but strongly typed API,
  • Arrange-Act-Assert (AAA) API.

In this post I want to show you how to mock the AppSettings property of the ConfigurationManager class using the Reflective and Natural Mocks patterns.

the Reflective Mocks way

reflectivemocks

Notice the ClearMocks attribute (line 16) on the testclass. It tells TypeMock to cleanup its mock administration before a test is run. Also notice the VerifyMocks attribute (line 20) on the testmethod. This attribute cause TypeMock to verify the mocks in the test. These attributes are used by Natural Mocks also.

MockManager.Mock (line 29) is used to get fake of the ConfigurationManager. With the fake you can tell TypeMock that the AppSettings property returns a NameValueCollection. You can use either ExpectGet or ExpectGetAlways. I used the latter because TypeMock will always return the value you setup. ExpectGet will only return the value once. Every next call to AppSettings will throw an exception.

As you can see in the example above I created a NameValueCollection and added the key-value pair “ApplicationName”- “TypeMockDemo”. With ExpectGetAlways (line 30) I make sure the my collection is always returned. Just the way I like it.

This is all you have to do to setup ConfigurationManager.AppSettings. Now you can call the code you want to test like you always did. Look at the picture below for the implementation of the ApplicationContext class.

appContext

The Natural Mocks way

Next the same example as above but now implemented with Natural Mocks. The picture below shows the code:

naturalmocks

The setup of the fake is done in the using block. A recorder is created by RecorderManager.StartRecording. The recorder let you setup expectations. In this case I told TypeMock that I expect a call to the  ConfigurationManager.AppSettings.Get method. See line 45. After setting up the expectation I told the recorder to return “TypeMockDemo” for this expectation. After the setup you can call the code you want to test like you always did.

I do not like line 45 because it is not natural. I want to setup the expectation like this: ConfigurationManager.AppSettings[“ApplicationName”] = “TypeMockDemo“;. I could not get that to work. Maybe someone else knows how to do that. Someone?

Finishing up

I am going to show more examples of how to mock using TypeMock. Also I am going to rewrite the examples with RhinoMocks and Moq mocking frameworks.

Updated: The pictures were wrong. I copy-pasted the pictures. If you do that Live Writer gives them the same name. My blog shows the same image for all images. I corrected that.

Shifter has been updated. It supports constructor, field, property and method injection now. Alot of work needs to be done still, though. I will continue my work and make some examples. Documentation needs to be made also but who likes documenting?

This evening I tried to connect to my TFS server. But there was a problem. Why? I thought. It was working last night. Luckily I found the problem after a little while. I want to share my solution with you because when I searched the internet I found a ‘solution’ that did not work. This solution wanted me to add the TFSService account to ‘C:\Users\All Users\Microsoft\Crypto\RSA\’. Maybe a solution to a problem but not mine.

But let me begin at the beginning. When connecting to my TFS server I got the following error message: TFS30059: Fatal error while initializing web service. Look at the picture below:

   

The error message is clear. It should be easy to solve it. I googled to see what the solution to the problem was. As I mentioned before  I found some posts to change the security settings of the RSA folder and add the TFSService account. I tried it but it did not work. I started to look for a solution on my own. I tried IIS. Maybe it was offline or maybe the Team System Server webserviceswere offline. No, that was not the case. Then I thought that the Team System databases could be the problem. So I tried to connect to my Sql Server instance. I could not connect and I got the message to look at the configuration withthe ‘Sql Server Configuration Manager’ (see the picture below). On the Service tab I saw the problem. My Sql Server instances were offline. I restarted them and all was fine. :^)

 

Not so long ago I heard that it was impossible to unshelve a shelveset from the branch it was shelve on to a different branch. It is the way Team Foundation Server (TFS) works I was told. And indeed it is the way tfs works out of the box. I asked a colleague and he researched it and gave me the same message:  you cannot do it. He is right but I was not satisfied. I needed to find a solution. I either would write it myself or I would find an existing tool. And I found it. It can be done and the way to do it is with the unshelve command of the powertools for tfs.

The commandline of the unshelve command of this tool is:

tfpt unshelve – Unshelve into workspace with pending changes

Allows a shelveset to be unshelved into a workspace with pending changes.
Merges content between local and shelved changes. Allows migration of shelved
changes from one branch into another by rewriting server paths.

Allows changes from an already-unshelved shelveset to be undone, cleaning
up pending adds, and preserving other existing pending changes in the
workspace.

Usage: tfpt unshelve [shelvesetname[;username]] [/nobackup]
                              [/migrate /source:serverpath /target:serverpath]

 shelvesetname           The name of the shelveset to unshelve
 /nobackup                 Skip the creation of a backup shelveset
 /migrate                    Rewrite the server paths of the shelved items
                                 (for example to unshelve into another branch)
 /source:serverpath     Source location for path rewrite (supply with /migrate)
 /target:serverpath      Target location for path rewrite (supply with /migrate)
 /undo                        Undo pending changes from an unshelved shelveset
 /batchsize:num          Set the batch size for server calls (default 500)

 

Yesterday I discovered  a podcast about Team Foudation Server (TFS). It’s called Radio TFS and it can be found here. The hosts are Paul Hacker, Mickey Gousset and Martin Woodward. They are all Team System MVPs.

Paul Hacker is involved with TFS Times, also. TFS Times is kind of newspaper about TFS with technical information. Mickey Gousset started Team System Rocks. Martin Woodward is a senior software engineer for Teamprise .

I have listened to the first four episodes and it sounded very good. The podcast is presented very well and contains lots of good technical information. You must listen to it!

 

Note: I blog about TFS here, also.

Debugging helps to understand how code works and what causes a bug. But it is not always easy to debug an application. Stepping into a method with parameters, for instance, can be difficult because you will first step into all the properties that are passed as arguments. You solve this problem by jumping to the definition of the method, setting a breakpoint in the method and pressing F5. You can use ‘Run to cursor’ also, of course. But if you have control over the source code you can use the DebuggerStepThroughAttribute to prevent the debugger stepping into the properties.

 The DebuggerStepThroughAttribute does not mean anything to the compiler. It is only there for debuggers. You can mark a method, a constructor, a class or a struct with it. The debugger will not step into the decorated construct. You can still set a breakpoint in marked constructs.