Dennis van der Stelt

The only way to win is to learn faster than anyone else

Community

Email Notifications

News

  • Addicted to Refactor! Pro

I read...

I Use...

Tags

Recent Posts

Archives

Blog Subscription Form

  • Email Notifications
    Go

September 2008 - Posts

Typemock Isolator : Getting internal variables

Sometimes you execute some method and the method returns void. But it still does some complicated stuff and you want to check if everything worked. But you can't access the internal variable that was created. You can mock everything away and make sure you set the proper return values. But there's also another way to achieve about the same result.

Imagine we have a customer class.

public class Customer

{

  public string Name { get; set; }

  public int Age { get; set; }

}

We're also having a class that internally creates a customer and works with it a little.

public class ClassToTest

{

  public void Somemethod()

  {

    int[] ages = new int[] { 33, 34, 35 };

 

    Customer c = new Customer();

    c.Name = "Dennis van der Stelt";

    c.Age = ages.SkipWhile(i => i < 34).First();

  }

}

As you can see, normally it'd be impossible to test what happened to the customer object. Unless you're using TypeMock Isolator. Here's how.

    1 [TestMethod]

    2 public void TestExample()

    3 {

    4   MockManager.Init();

    5   Mock mockedCustomer = MockManager.Mock<Customer>(Constructor.NotMocked);

    6 

    7   ClassToTest ctt = new ClassToTest();

    8   ctt.Somemethod();

    9 

   10   Customer c = mockedCustomer.MockedInstance as Customer;

   11   Assert.AreEqual<string>("Dennis van der Stelt", c.Name);

   12   Assert.AreEqual<int>(34, c.Age);

   13 }

In line 5 we're telling Isolator to mock the object, but we're not setting any expectations. Therefor nothing is hidden or mocked away. We're even telling the constructor to be executed normally. In lines 7 & 8 we execute the method. In line 10 we're getting the instance that was created inside our tested method and after that, we can assert its values in line 11 and 12.

Now image that the age.SkipWhile() method was some call to another class or even the database. We'd definitely want to mock that out. But we're already using reflective mocks. Although more powerful, it's so much easier to work with natural mocks. Here's how.

    1 [TestMethod]

    2 public void TestExample2()

    3 {

    4   MockManager.Init();

    5   Mock mockedCustomer = MockManager.Mock<Customer>();

    6 

    7   int[] someArray = new int[] { 1, 2, 3 };

    8   using (RecordExpectations recorder = RecorderManager.StartRecording())

    9   {

   10     someArray.SkipWhile(i => i < 2).First();

   11     recorder.Return(5);

   12   }

   13 

   14   ClassToTest ctt = new ClassToTest();

   15   ctt.Somemethod();

   16 

   17   Customer c = mockedCustomer.MockedInstance as Customer;

   18   Assert.AreEqual<string>("Dennis van der Stelt", c.Name);

   19   Assert.AreEqual<int>(5, c.Age);

   20 }

As you can see in line 7 I'm creating a temporary integer array so I can use the SkipWhile extension method on it. It holds completely different values however. In line 11 I set the actual return value, again completely separated from anything else I've created before. I tell it to return the value 5 and that's what I'm asserting later in my test on line 19.

But what if more instances of the Customer class are created? That's also very easy, because the natural mocks work exactly like the real code it passes. As you can see in the example below on line 5 and 6 I expect two Customer classes to be created and set the resulting mocked object to different variables. In line 15 and 16 I retrieve the second Customer object and assert its values.

    1 [TestMethod]

    2 public void TestExample()

    3 {

    4   MockManager.Init();

    5   Mock mockedCustomer = MockManager.Mock<Customer>(Constructor.NotMocked);

    6   Mock mockedCustomer2 = MockManager.Mock<Customer>(Constructor.NotMocked);

    7 

    8   ClassToTest ctt = new ClassToTest();

    9   ctt.Somemethod();

   10 

   11   Customer c = mockedCustomer.MockedInstance as Customer;

   12   Assert.AreEqual<string>("Dennis van der Stelt", c.Name);

   13   Assert.AreEqual<int>(34, c.Age);

   14 

   15   Customer c2 = mockedCustomer2.MockedInstance as Customer;

   16   Assert.AreEqual<string>("Eli Lopian", c2.Name);

   17 }

Microsoft project “Velocity”

Thursday September the 4th I gave a presentation for DotNed about Velocity, the framework to enable distributed cache on the Windows platform. It was a fun evening for me with lots of great questions from the audience. You can download the slides from the site.

There were a few questions that I couldn’t answer though.

Regions 
I know assumptions are the mother of all censored, but I made the assumption that regions aren’t being replicated across hosts but stay in one host. It’s not entirely untrue, but it is a little more complex though.

When you specify a region in configuration or in the .Put() method calls, all objects in that region won’t get replicated or partitioned across hosts but will stay on a single host, also called a node. However if you don’t specify a region explicitly, the objects will get spread across nodes. However, Velocity will still create regions. And that’s what we were wondering about during the course where I explained I didn’t fully understood what was going on there. Well, the regions being created will (or might) hold a copy of your object and this way your object will get partitioned across hosts and preferably across machines.

What about security?
Velocity enabled caching on enterprise scale, resulting in a cache distributed over possible hundreds of machines, accessed by a great number of applications. In CTP1 there’s no way to secure your data, which is likely a problem for a number of scenarios. The team just posted that for version 1 of Velocity they’re going to implement application level security using token level security and/or take the applicationid/siteid from ASP.NET to use that to secure access. Read more in this thread on the forums.

Still only 38 days, 16 hours and 37 minutes to PDC, hope to hear more about Velocity there.

FinalBuilder : Action lists

As I’ve said before, years ago on some project I first got the opportunity to work with FinalBuilder. After years of silence I picked it up again at version 5 and recently switched to the latest version 6.

Have you ever had the feeling that some developer around you suffered from the NIH syndrome? Like myself on the pdf creation?! And that they’re wasting a humongous amount of time building it themselves instead of using some $300 tool?

If you still work with MSBuild, you’re suffering from the NIH syndrome

Setting up a FinalBuilder script can take some time, especially for the first time, but not as much as with MSBuild. And when you’re done, it so extremely easy to change the script real fast. Seriously, try it out. It’ll enhance your daily life.

But back to action lists. Today I found out about them. FinalBuilder is so chuck-full of actions and possibilities that you can’t learn them all at once. But that’s where the blogosphere comes to the rescue.

actionlist Ever noticed the two tabs “Main” and “OnFailure”? The first one is where your actions are and the second one is executed on failure. It’s awesome to use for sending an e-mail or creating TFS workitems when something went wrong.

But looking at the image on the right you might already noticed I’ve created a third and custom action list. This is like a subroutine you can execute, just like in your code. You can even provide it with a set of parameters that you can set from the “Run Action List” action, as shown in the image. Right-click the tab to rename it or specify the available parameters. I’ve added a “MessageBox_Message” parameter so I can show a MessageBox with a custom message.

actionlist2

After you’ve specified some parameters, you can set them within the mentioned “Run Action List” action, as shown below.

actionlist3

When I get back to work tomorrow, I really have to implement this in one of our most complex build scripts.

  1. It cleans up the “General” action list
  2. It takes parameters without having the need to specify extra project variables/properties.
Oslo, D and the PDC

There’s an article up at eweek about Oslo, the new initiative by Microsoft in create the tools and a language that’s more accessible to more people than just official developers. You’re supposed to create data and models to build an application, something where the business analyst can already do a lot of work, where he now transfers a large document over to the development team.

I’m really interested in hearing more about Oslo at the PDC next month, and Erwyn van der Meer keeps triggering me and others by telling (over chat and blogs) that we’ll like what we’re going to see at the PDC. I’ve already heard some stuff and I can’t wait.

I’m really curious however that if they’re going to make it more accessible to non-developers, how are ‘real’ developers going to like it. As far as I understand the new language ‘D’ is for those developers but how about the modeling tools? From day one that I got into computers people have told me that in the future you’d just put some blocks together, tweak a bit here and there in configuration and you’ll have some application. While that might sound interesting to a lot of people, I got into programming when I was 10 years old and haven’t stopped coding since. I do my work because I love coding. Will Oslo and ‘D’ change this?

We’ll see. According to the site, 47 days, 3 hours and 11 minutes to go. :-)

Internet Explorer 8 Beta 2 experiences

I just uninstalled Internet Explorer 8 Beta 2 because it constantly kept crashing. A good thing was that it was only crashing a single tab in IE8, but multiple tabs constantly kept crashing for no reason (to me at least).

A new nice feature is colored tabs. But the behavior also changed. Normally I could drag the icon of a website from the address bar to the active tab or drag a link from a page to the active tab. That has been taken away and I question why. Maybe because it’s beta, maybe because Microsoft doesn’t understand it. Since I can remember Firefox supports way more advanced tabs. I can drag URLs to any tab and open the website in that tab, without losing focus of the current active tab. I can also close inactive tabs without opening it first. I really missed those features in IE7 but IE8 seems to make it worse.

Anyway, I’ll keep on using IE7 and hope for the best in the future…