April 2008 - Posts

Recently , we have added a new section at dotnetslackers by which you can share your code to public.  Now, code snippets are tiny piece of code that can work as a unit, can come handy and be a real time-saver when building up complex solution or can be some interesting piece of lines which in turn can make out someone's day.

To get started, lets go to  www.dotnetslackers.com 

image

Navigate to Community -> Code snippets. Here, either you can browse snippets posted by others and share your comments or hit on the "Add new" button to make one by yourself. It will firstly direct you to the login page, if you are not logged in already. Finally, you will get a clean form to add your snippet and give some title, detail and tags for it and its done.

image

Let us know, how do you feel about the new feature and what are the things you would love to see which will make it work for you. Anything you want to share, just write them down at

http://dotnetslackers.com/community/forums/t/1823.aspx

Enjoy!!

kick it on DotNetKicks.com

There are mocking tools like Rhino Mock, Moq, NMock, Typemock and many more. Recently, I was kinda evaluating mocking tools to mock out REST calls in Linq.Flickr. Almost all of the mock tools requires some sort of interface reference to work on. When, to mock out methods like XElement.Load or File.Open is impossible with most of the tools, Typemock is one step ahead of others as it supports mocking of static methods.

Since, all the queries in Linq.Flickr goes through REST and all is tied with somehow with XElement.Load for getting the response in a LINQToXml fashion (I have mentioned already in my earlier posts, how :-)). I wanted to fake out the HTTP layer completely so that sitting in an airplane and with no Internet it is possible to test the product.

Before going to any downloads , lets see how easy is Typemock in this regard.

Now, all starts with a NUnit test class, where I have referenced the Typemock lib and during initialization of the test I faked out XElement.Load method for particular URL request to Flickr. Fragment of the code could be like

[SetUp]
public void Initialize()
{
   ...
   ...
   // initialize mock 
    MockManager.Init();

    string frobUrl = "http://api.flickr.com/services/rest/?       
    method=flickr.auth.getFrob&api_key=xxxx
    &api_sig=cdeb9a73a771d5ea374d014680b33a0e";
    // custom method to get element locally
    XElement element1 = MockElement(frobUrl, "LinqToFlickrTest.GetFrob.xml"); 
    // create the mock object.
    Mock mock1 = MockManager.Mock(typeof(XElement));
    mock1.ExpectAndReturn("Load", element1).When(frobUrl);
   ...
   ...
}

So, I created a mock object of Type XElement and set it up for an URL so that when request comes in, it will return my response instead of looking into the sky for it. Finally, its all plain old test class, with no suspicious code :-)

[Test]
public void Test()
{
    var query = from photo in _context.Photos
                where photo.SearchMode == SearchMode.TagsOnly 
                && photo.SearchText == "microsoft"
                select photo; 

    int count = query.Count(); 
   ...
   ...
}

Additionally, its nice to check if all the mock worked out fine in TearDown by MockManager.Verify

[TearDown]
public void Verify()
{
    MockManager.Verify();
}

That's it , you can download a working sample right here. If you have Typemock and NUnit installed, I have provided a .BAT for running NUnit through TMockRunner, change the paths accordingly to have it work for you.

In the end, it is worth mentioning that currently Mock.When is only supported in Typemock enterprise edition.Therefore, I will also wrap the dateaccess layer of Linq.Flickr with interface reference so that it is possible to mock with others mocking tools as well and I will keep the code updated with latest changes so that you can play around it.

kick it on DotNetKicks.com

Just a few days ago I have rolled up another patch for LinqExtender which is 1.3.2 , this is reported to me from a work item, especially to support the following query

var query = from book in _context.Books
            where book.PublishedDate > someDate 
            and book.PublishedDate < someDate
            select book;

Here we can see that there are similar property is used for different segment in where clause. Now, previously this kind of query used to fail tests as bucket.Items["SomeProperty"].Value and bucket.Items["SomeProperty"].RelationType only returns single item value and relation type respectively. Therefore, I have added a new property that will be used to handle multiple homogenous query conditions and which can be used  like the following

IList<BucketItem.QueryCondition> conditions  = bucket.Items["SomeProperty"].Values

foreach (BucketItem.QueryCondtion condition in conditions)
{
  // do something precious with the value.
  obj value =  condition.Value;
  // contains less, equal, etc.
  RelationType rel = condtion.RelationType; 
}

In case, your data source don't have that capability for doing this kind of query , and you decide to stick to the single item getter, then the provider will gracefully give "not supported" exception rather than quietly failing the unit test :-). Again, its up to the user how they use it and hope it comes handy.

kick it on DotNetKicks.com

I just came to know that I have been given the MVP award for 2008 that comes through the following line from a mail in my inbox.

"Congratulations! We are pleased to present you with the 2008 Microsoft® MVP Award! The MVP Award is our way to say thank you for promoting the spirit of community and improving people’s lives and the industry’s success every day"

Formally, this award goes to my numerous readers, people who showed their support by downloading my projects, asking for fixes and reading my boring articles :-)  and more ...

Finally, don't forget to try out the new release of LinqExtender, I will post the detail in coming posts and few weeks ago I mentioned in my blog about Flickr MVC project with Linq.Flickr that is coming to the end so watch it out as well.

Technically, I am an MVP in Asp.net , but it could be nicer if there is one in LINQ as LINQ is common to both Asp.net and Windows clients. But in the end, recognition really makes thing better :-)

More Posts