Monday, March 31, 2008

Wii Have a Wiinner!

That's right. Narcisse Adjalla is the winner of the Wii sweepstakes in our last webcast. He's now the proud owner of a new Nintendo Wii.

Narcisse works at SoftTarget inc., a company that develop solution in portfolio modeling, pre-trade compliance, portfolio manufacturing, workflow automation, reporting and integration. SoftTarget’s principal soft product is iBalance for portfolio management.

Narcisse in his own words:

I’m an analyst Programmer with SoftTarget Inc. My principal work consist on modeling portfolio, generate recommendations using multi-factor targets (weight, duration, currency etc.) and constraints. I also develop a lot programmer tests because I manipulate sensitive data. Therefore, Typemock help me principally in mocking database data.

I graduated At polythechnique school located in Montreal in computer engineering and now I’m finishing a Master in Applied Mathematics in the same university.

So I guess these computer engineering classes paid off.

Congratulations Narcisse!

Typemock acquired by Microsoft

Update: If you didn't realize this, this was an april fool's post, nothing more (yet!). Do enjoy the movie though. it is from our 4.2 release pool party!

I'm happy to announce that last week, Typemock has accepted an offer to be acquired by Microsoft Corp.  (Here is the post on TechCrunch )Expect a proper press release later this afternoon.

The documents have been signed today and we have ben celebrating all this past weekend: see video below

The move comes as no big surprise. after Microsoft has released its unit testing tools, it was only a matter of time until they introduced a Mocking framework into the toolset which allows testing both new code and existing legacy code for organizations which face this problem. That is where Typemock Isolator comes in, and wins the match.

The product is expected to be available in future versions of VS.NET Professional, Team Test, Team Developer and Team Suite, and will have close integration with the MS testing tools.  This is all we know at the moment and will update as things progress over time.

It's been a fun ride, and we're not even sure if development will move all or partly to Redmond or stay in Israel. In any case, it's going to be interesting :)

Thursday, March 27, 2008

Decisions, Decisions...

As you probably know, (it was in our release notes, and everybody reads those...) we are removing the support for .Net 1.1 in the next version of Typemock Isolator. This decision was not easy, as we know some of our customers use VS2003 to develop and test their code. On the upside, some of the problems customers have been experiencing (and if you ever saw the dreaded BadImageFormatException, you know who you are) will magically disappear.

And there was much rejoicing in the development team room. Because now our build process is much simpler. The work includes updating the solution and projects, test scripts, and the installer building scripts. Removing some files and folders from the installer was easy. We use Wix and WixEdit for that. But one of the unforeseen events that came up was that NDoc is now failing. Before it had a 1.1 version to work on, not it doesn't.

Now, for those of you familiar with NDoc, I hear you say: Well - NDoc doesn't support .Net 2.0!

You are right. We are in the process of examining new documentation tools. But what do we do until then? If we leave the installation script as it is, we lose our ability to demo a fully working version at the end of the iteration.

We decided that for now, we'll compile a separate 1.1 version, just for the sake of the almighty NDoc. It's not a permanent solution, and obviously not one we like, but it is important for our incremental process.

Sometimes we need to do things we don't like, or not aesthetically pleasing. We do so because we value other things more. What kind of "ugly" paths have you taken for the sake of your values?

Wednesday, March 26, 2008

Great introduction tutorial for Typemock Isolator

I just wanted to let you know that Stephen Walther wrote an excellent introduction tutorial for Typemock Isolator.
The tutorial has emphasis towards Data Base testing.
You can read it here.  

Tuesday, March 25, 2008

MbUnit "Gallio" announces built in support for Typemock Isolator

Jeff Brown Just posted about the current alpha version of MbUnit (Gallio codename) with cool new features. I urge you to take a look at it here.

Cool thing about it: Built in integration for Typemock and NCover runners:

"This is all it takes to run your tests with NCover or TypeMock using MSBuild:

<Gallio Assemblies="MyAssembly.dll" RunnerType="NCover" />
<Gallio Assemblies="MyAssembly.dll" RunnerType="TypeMock" />

The trick is that the tests will run in an isolated host process with the profiler attached.  You can of course plug in your own runners too.  You can of course pass the same arguments to the NAnt task, PowerShell task and the command-line runner (Echo).

Magic. "

Coolness. Great job.

Friday, March 14, 2008

Is Typemock Isolator Like A Credit Card?

Well, no. However, this post from The Simple Dollar reminded me so much of the discussion whether Typemock Isolator is too powerful, or can be overused. (If you want a rehash of that discussion, you can find the links on Eli's blog in this post, and the InfoQ post.)

The post talks about the holy war between those who believe credit cards are evil, and those who believe they are a blessing. Here's a quote from the Simple Dollar post:

"I look at credit cards as being like a very dangerous power tool. If you’re careful and take the proper precautions, they can save you time and shower some rewards on you as well. On the other hand, if you use credit cards with reckless abandon, you run the serious risk of some intense financial damage to yourself."

Now replace "credit cards" with "Typemock Isolator". And "financial" with whatever is relevant for you. Sounds familiar, isn't it? I'm not going to go into this discussion again, though.

I want to take a different spin on the analogy.

I would like Isolator to be like credit card. It should be:

  • Simple to use
  • Reliable
  • Accessible to most people (And I do consider most developers to be people)
  • Allow you to use it in which way you desire (as long as you understand the consequences)

I would also like Isolator to do the following: whenever I enter a license key, it will give me money. But for some reason, it is currently at the bottom of the feature backlog...

Tuesday, March 11, 2008

Chains, part III: Extension Methods

This post continues the path of part I and II regarding chained method calls using Typemock Isolator. Today I'm going to throw extension methods into the fray. And there's nothing better to start with than a great tradition - here's another pun:

Expectation Identifica-chain

Let's start with a simple call:

x = a.b();
Isolator has a trinity of sorts when identifying expectations: Instance, method and return value. It's more complicated than that, naturally, but let's keep it simple for now. So in the former case the trinity is:

instance: a
method: "b"
return value: x

Now, let's look at this differently. What if b() is not an instance method, but an extension method? In that case, it is really a static method, not an instance method. For static calls, Isolator uses null value for the instance. So the trinity would be:

instance: null
method: "b"
return value: x

Exten-chain Methods

Let's say we have the canonical chain:

a.b().c();

As we already know, this usually translates to:

tempReturn = a.b();
theReturn = tempReturn.c();

Suppose that the method c() is really an extension method. It looks like its invoked on the return value of tempReturn, but it is actually a static call of the class of tempReturn. It looks like this:

tempReturn = a.b();
theReturn = ClassOfTempReturn.c(tempReturn);

If we see a static call in the middle of a chain it is grounds for a chain break - the return value of the former call (tempReturn) is different than the target of the current call (which is null). Refer to the chain rules here.

Suspen-chain of Disbelief

So we need to maintain the illusion of the chain, although it is not really a chain. Isolator identifies a call as extension method via reflection - the method contains an ExtensionAttribute. For a method like this, we examine the first parameter (which for extension method is the "this" reference), and compare it to the return value of the former call. If they are the same we have a chain, and if not - we have a chain break.

This is chaining, for now. Too many chain puns can be hazardous to your health.

Reminder: Typemock Webcast Tomorrow

Just a reminder: Tomorrow, Wednesday March 12th at 4PM GMT, we are having a Webcast. The live demo will include:

  • The Support of the new .NET 3.5 framework and specifically how to mock LINQ statements.
  • The improved IDE based on using colors to emphasize mocked methods.
  • Seamless integration with Microsoft’s Visual Studio 2008.

And you'll have a chance to chat with the Typemock team online. If you complete our survey, (and you're lucky) you maybe the one to win a Nintendo Wii!

To participate in the web cast click here.

See you tomorrow!

Sunday, March 9, 2008

Freedom of choice

Many discussion has been made about Designing for Testability, and why Typemock is too powerful. The most recent of them was made by Mark.

When I read most discussions I usually find that the underlying assumption is that Design for testability is better.
Well is it? (probably yes) But is it the BEST design approach? well maybe, But maybe not!
For most people however the last question is purely academic, Most mocking frameworks will limit the developer into designing in one particular way, so why bother contemplating the theoretical?

The real added value of using Typemock is the freedom to choose. Using Typemock (for greenfield development as well) will allow each developer to actually consider design alternatives and give him the freedom to choose between them. There might be a better option out there. And even if we cant find it I will always feel that just by searching I ended up in a better place.

In his post mark asks:

"why would I want to waste my brain power constraining myself
instead of having a tool do that?"

And I answer: brain power cant be wasted. Keeping your thoughts open for new alternative is at worst a good exercise for the brain allowing it to become better. From time to time new and better approaches will be found.

Mark later describes his experience when starting out TDD. My road was also quite similar, I also learnt along the way how to design my code so it can be tested and i become a better developer for it. But unlike mark I do not feel that the road has ended. Far from it, I feel that I have only started but I wont make any progress if i wont open my mind to other possibilities.

"If you approach the tool with an open mind, it can help you!"

Of course. That exactly my point. Give Typemock the chance and you might learn better ways to design your code.

Testing MVC Controllers in 3 lines of code with Typemock Isolator

Here's a much simpler version of how you'd test an ASP.NET MVC Controller using Typemock, without needing any helper class to do it:

[TestMethod,VerifyMocks]
  public void Hello_ShowsTheHelloView2()
  {
      Mock mock = MockManager.Mock<HomeController>();
      mock.ExpectCall("RenderView").Args("Index");


      HomeController controller = new HomeController();
      controller.Index();
  }

This is possible because we can mock out the protected (non virtual ) method "RenderView" on the Controller itself, which saves us the trouble of even touching anything underneath it.

With this, you don't need to have any of the helper code we wrote about here. If your controller uses the request context however, you may want to still mock that out using the previously mentioned links .

Saturday, March 8, 2008

Typemock Isolator Vs. Rhino.Mocks

One of the things people keep asking is "so why should I pay for a tool that I can get for free?" in other words, what is so special that you'd actually want me to pay you for.

This of course, is because there is no clear comparison chart between typemock and other free and open source tools such as rhino mocks.

There are many ways to compare frameworks, but the first and easiest one would be feature by feature comparison. How fair\relevant is it? you be the judge. I'm putting this chart on this blog so that we can gather comments on what you think would constitute a fair and balanced comparison chart between the various mocking frameworks, and this is something to start with.

If you have objections, suggestions or any other type of feedback, feel free to put in a comment. ('y' = 'supported' , '-' = 'not supported')

(this chart relates to the enterprise version of Typemock Isolator. for a chart detailing the differenced between typemock versions and to understand what the features listed here mean, look over here.)

 

Feature Category Feature Detail Typemock Isolator Rhino.Mocks
.NET Framework support      
  1.1 y

yes - (only the 2.9.6 version )

  2.0 y y
  3.0-3.5 y y
Syntax      
  Record-Replay y y
  Expectation based y y
What can it mock?      
  public method y y
  properties y y
  events y y
  interface y y
  Abstract Classes y y
  void calls y y
Non-Classic Mocking Future Object instance y --
  Static methods y --
  Extensions Methods y --
  LINQ Queries y --
  Anonymous Objects y --
  Field set-get y --
  Constructors y --
Parameter Constraints      
  Argument matching y y
  Custom Callbacks y y
  Built in constraints y y
IDE Support      
  Highlight mocked methods in debugger y --
  Evaluate mock values safely in debugger y --
other features      
  strict\non strict mocks y y
  strongly typed calls y(except recorder.return() ) y
  Extensibility mechanism for tests y --
  support 'stub' keyword -- (stubs are supported but no keyword for it) y
  Runtime argument swapping y --
  partial mocks y y
       
       
       
       

Testing ASP.NET MVC using Typemock Isolator

Update: There is a much simpler version of these tests that does not even require any helper classes. You may want to look at that as well.

 

Scott Hanselman posted his notes and slides (and movies!) from his talk on the ASP.NET MVC framework, where he also mentioned and shows how he's write unit tests for the MVC framework in ASP.NET.

Scott has examples both in Moq and Rhino.Mocks on how to write these tests, so it should be no surprise we'd want to get into the game.

You can download a zip file that contains the Typemock version of the Mvc mock Helpers here. This version should work with the enterprise version of Typemock, but we'll make a community edition available soon.

Here is the full source code of the mocking helper class.

 

using System;
using System.Collections.Specialized;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using TypeMock;

namespace Typemock.Mvc
{
static class MvcMockHelpers
{
public static void SetFakeContextOn(Controller controller)
{
HttpContextBase context = MvcMockHelpers.FakeHttpContext();
controller.ControllerContext = new ControllerContext(new RequestContext(context, new RouteData()), controller);
}

public static void SetHttpMethodResult(this HttpRequestBase request, string httpMethod)
{

using (var r = new RecordExpectations())
{
r.ExpectAndReturn(request.HttpMethod, httpMethod);
}
}

public static void SetupRequestUrl(this HttpRequestBase request, string url)
{
if (url == null)
throw new ArgumentNullException("url");

if (!url.StartsWith("~/"))
throw new ArgumentException("Sorry, we expect a virtual url starting with \"~/\".");
var parameters = GetQueryStringParameters(url);
var fileName = GetUrlFileName(url);
using (var r = new RecordExpectations())
{
r.ExpectAndReturn(request.QueryString, parameters);
r.ExpectAndReturn(request.AppRelativeCurrentExecutionFilePath, fileName);
r.ExpectAndReturn(request.PathInfo, string.Empty);
}
}

static string GetUrlFileName(string url)
{
if (url.Contains("?"))
return url.Substring(0, url.IndexOf("?"));
else
return url;
}
static NameValueCollection GetQueryStringParameters(string url)
{
if (url.Contains("?"))
{
NameValueCollection parameters = new NameValueCollection();

string[] parts = url.Split("?".ToCharArray());
string[] keys = parts[1].Split("&".ToCharArray());

foreach (string key in keys)
{
string[] part = key.Split("=".ToCharArray());
parameters.Add(part[0], part[1]);
}

return parameters;
}
else
{
return null;
}
}
public static HttpContextBase FakeHttpContext(string url)
{
HttpContextBase context = FakeHttpContext();
context.Request.SetupRequestUrl(url);
return context;
}
public static HttpContextBase FakeHttpContext()
{
HttpContextBase context = MockManager.MockObject<HttpContextBase>().Object;
HttpRequestBase request = MockManager.MockObject<HttpRequestBase>().Object;
HttpResponseBase response = MockManager.MockObject<HttpResponseBase>().Object;
HttpSessionStateBase sessionState = MockManager.MockObject<HttpSessionStateBase>().Object;
HttpServerUtilityBase serverUtility = MockManager.MockObject<HttpServerUtilityBase>().Object;
using (var r = new RecordExpectations())
{
r.DefaultBehavior.RepeatAlways();
r.ExpectAndReturn(context.Response, response);
r.ExpectAndReturn(context.Request, request);
r.ExpectAndReturn(context.Session, sessionState);
r.ExpectAndReturn(context.Server, serverUtility);
}
return context;
}

}
}



And here is an example of how you can write tests with it:




 



using System.Collections;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Routing;
using MvcApplication1;
using MvcApplication1.Controllers;
using NUnit.Framework;
using Typemock.Mvc;

namespace MvcAppTests
{
[TestFixture]
public class HomeControllerTests
{
[Test]
public void Hello_ShowsTheHelloView()
{
FakeViewEngine engine = new FakeViewEngine();
HomeController controller = new HomeController();
controller.ViewEngine=engine;
MvcMockHelpers.SetFakeContextOn(controller);

controller.Hello();
Assert.AreEqual("Hello",engine.CurrentViewContext.ViewName);

}

[Test]
public void RouteDefaultsWork()
{
var app = new GlobalApplication();
RouteCollection routes = new RouteCollection();
GlobalApplication.RegisterRoutes(routes);
var context = MvcMockHelpers.FakeHttpContext();
context.Request.SetupRequestUrl("~/home");

var data = routes.GetRouteData(context);
Assert.AreEqual("home",data.Values["controller"]);
Assert.AreEqual("Index",data.Values["action"]);
}
}

internal class FakeViewEngine : IViewEngine
{
public ViewContext CurrentViewContext;
public void RenderView(ViewContext viewContext)
{
CurrentViewContext = viewContext;
}
}
}


Thursday, March 6, 2008

Chains, Part II: Chain Rules

On Part I, I described what chains are, and the challenge they pose for Isolator. As you've already gathered, I will use every opportunity to use lame puns, so the word of the day is:

Interpreta-chain

How does Isolator interpret chains?

Well, first let's see how it interprets method calls in general. Isolator uses the profiler API to intercept calls by the CLR. Whenever a method call is invoked, Isolator intercepts it first and does one of the following operations:

  • Call the original method
  • Record an expectation
  • Return a mocked value

Since this is already compiled code, there's no signaling of an end of line. The only information it receives is the next call.

Let's examine this code:

using (RecordExpectations rec = RecorderManager.StartRecording())
{
int x = a.b().c();
rec.Return(5);
}
Isolator receives the calls in this order (the assignment is implicit):

XReturn = a.b()      // returns a default "temporary" return value XReturn
int x = XReturn.c() // returns a default "temporary" return value x
rec.Return(5) // substitutes the former x with 5.

Now look at this chain:
int x = a.b().c(d.e(f.g().h(2,i.j()));

Can you guess which 3 method calls are invoked first? If you guessed a.b(), f.g(), and i.j() in this order, you are correct. You are probably also suffering from the same headache I had when trying to dive into chain land. So method calls come in a somewhat confusing order, until you understand the rules.

Identifica-chain

A chain has a head and a tail, which identify it. Knowing when a chain starts is easy - with the first call. But where does it end?

Let's look at this code:

using (RecordExpectations rec = RecorderManager.StartRecording())
{
int x = a.b().c(d.e(f.g().h(2,i.j()));
rec.Return(5);
}
The writer of this code, evil-minded as he is, concentrates on one thing - returning 5 instead of the call to a.b().c(...) method. No expectations are set or examined on the other calls that are on the way, and that starts with a.b(). Rule #1 Isolator uses the Return call to identify the end of the chain, and set the return value on that last expectation.

Now let's look at the following simpler code containing an error:

using (RecordExpectations rec = RecorderManager.StartRecording())
{
x = a.b();
// Missing call to rec.Return(...)
int y = d.e();
rec.Return(5);
}
The writer of this code, expert as he is, forgot to put a Return call following the first call. Isolator continues to receive the calls, and needs to decide if the chain has been broken. Rule #2 to identifying a chain is when the return value of the former call, is the target of this call. In this case, the returned value of a.b() is an instance, while the next call d.e() is static. A static call has no instance and therefore we can identify a chain break.

And, in the former case, the chain break actually points to an error in the code. This helps us give the correct notification to the user.

Rule #2 is also a generalization of the following case:

using (RecordExpectations rec = RecorderManager.StartRecording())
{
a.b();
// No need to return a value, it just returns void
int y = d.e();
rec.Return(5);
}
Since a.b() is a void call, there's no need for rec.Return call. Rule #3 says: if a method returns void, it's a chain breaker.

Next time, I'm going to discuss extension methods, and how they fit in chains.

Tuesday, March 4, 2008

Trouble on our Website

Hi all,

Recently we have encountered issues with the hosting service of our website.
For some reason all attempts to send e-mails from the site are failing for no apparent reason.
We are doing our best to locate and fix the issue until we do be advised that:

  1. New people registering to the forums will encounter an error at the end of the process - We are monitoring new registration continuously and manually activate the account its just takes longer. If its urgent just mail us and we will do our best to make things go faster.
  2. Posting on threads in which people have chosen to be notified by emails also end with an error - The post however is published.
  3. If you are the one waiting for the email notification - Currently its not working, you will need to go the the thread and check it or subscribe to the RSS.

We do apologize for this and we will update you when the problem is solved.

Monday, March 3, 2008

Upcoming Event on March 12

Join us for a special Webcast on Wednesday March 12, 16:00 GMT
The live demo will include:

  • The Support of the new .NET 3.5 framework and specifically how to mock LINQ statements.
  • The improved IDE based on using colors to emphasize mocked methods.
  • Seamless integration with  Microsoft’s Visual Studio 2008.


It is also an opportunity to meet Typemock’s development team, ask questions online and share some tips.
.....and one of you can win a prize of a Nintendo Wii during in the webcast!

Looking forward to meet you,
The Typemock Development Team

To participate in the Webcast go here