Damian Mehers' Blog Xamarin from Geneva, Switzerland.

17Sep/084

Using Moq ExpectSet

I am trying out Moq, having used RhinoMocks a fair bit in the past.

I was having trouble using the ExpectSet method which verifies that a property has been set, and a google search found nothing that directly answered my question.  I wanted to know how to verify that the value set is what was expected.

It turns out that you need to define a callback in which you have the assertion to verify that the property has been set to the correct value.

[TestMethod]
public void TestAppleShiner() {

    // Mock the interface being passed to the class to be tested
    var fruit = new Mock<IFruit>();

    // Define the expectation that the Colour property will be
    // set to Green
    fruit.ExpectSet(f=>f.Colour).Callback(
        setColor=>Assert.AreEqual("Green", setColor)).Verifiable();

    // Run the test
    ApplePolisher applePolisher = new ApplePolisher();
    applePolisher.Polish(fruit.Object);

    // Verify that the test passed (note .Verifiable on the ExpectSet)
    fruit.VerifyAll();
}


public interface IFruit {
    string Colour { get; set; }
}

public class ApplePolisher {
    public void Polish(IFruit fruit) {
        fruit.Colour = "Green";
    }
}
 
After ExpectSet, call the Callback method giving the name of the variable to hold the passed in value, and then the assertion with regards to its value.
Filed under: unit testing 4 Comments
14Sep/081

BeebMC 2.3 Released

Download it here: http://www.beebmc.com/

Fixes a bug reported by a couple of people where although it launched properly, it would not play music.

Filed under: BeebMC 1 Comment
9Sep/083

TraceJS V2 R.I.P.

A couple of years ago I posted a small blog entry on a throwaway tool that I had created that uses the Debug API to log all JavaScript that Internet Explorer executes.

A year ago I decided to take a year off from consulting, mainly to spend a lot more time with our kids while they are still young, but also to have some time to explore some product ideas.

The first idea I wanted to explore was whether or not it might be possible to extend the idea I'd had in that older blog post to actually make a commercial product that would not only trace all JavaScript, but would also show call stacks, and show how often functions were executed.

Technically it was tricky because I wanted my custom JavaScript debugger to be loaded in-process into Internet Explorer, since I thought that this might speed it up considerably. The secret turned out to be that I needed to pass a special flag to IDebugProgramProvider2->GetProviderProcessData (the PFLAG_DEBUGGEE flag).

After a couple of months of development I finally got it all working.

You started it from the IE Tools menu:
image

This would create a separate TraceJS dialog: image

Then as you access a page that contains JavaScript the TraceJS Window shows the statements that are being executed:image image

Once you'd captured the JavaScript execution, you could replay the executed JavaScript, either step-by-step, or continually at a pace you define:image

Finally you could  drill-down to see which functions are called the most, and who calls them:image

I had it all working in my initial build environment, which was XP based.  Then I tested in Vista, and I couldn't get it to work  (the GetProviderProcessData failed).  Ouch!

I took a step back, gave the product a long hard look, and decided to stop development.  It was too slow (despite being entirely in-process), I couldn't implement features I needed such as timing (the debugging/tracing added too much overhead), and I couldn't get it working on Vista.

Now that IE8 has similar profiling features I'm happy I stopped when I did.

Of course the biggest mistake I made was to register the domain names for the product before it was completed (tracejs.com, jsprof.com, jsprofiler.com and javascriptprofiler.com are all mine).

If there is one sure way to kill a product idea it is to register domain names before you've executed on the product idea itself :-).

Filed under: Debugging 3 Comments
7Sep/082

Using ASP.NET MVC and the AJAX Control Toolkit

There are a whole range of useful AJAX behaviors available in the ASP.NET AJAX Control Toolkit, and it would be cool to be able to use them from ASP.NET MVC. 

In this post I'll show you how you can use one of the extenders that I created, the ListSearch Extender, with ASP.NET MVC.

Set up your ASP.NET MVC Project

To start create a new ASP.NET MVC project.

Next download the Microsoft AJAX Library, which contains the Javascript library used in the Microsoft ASP.NET AJAX implementation. Copy the MicrosoftAjaxLibrary folder in the ZIP you downloaded to the Content folder in your MVC project.

Now download the Script Only Files from the ASP.NET AJAX Control Toolkit and copy the AjaxControlToolkit folder in the ZIP to the Content f0lder in your MVC project.

Now that the folders are copied you need to make them visible to the project.  Open the Content branch of your project in the Solution Explorer, and then click on the Show All Files button:

 

Once you do this, you should see the two folders that you copied into the Content folder.  Right click on each folder and select Include In Project.

Now your MVC project will have access to the JavaScript files it requires.

Add the SELECT

To make use of these JavaScript libraries, open the Home action's Index view under Views->Home->Index.aspx.  Modify the HTML to include a simple SELECT List:

<%@ Page Language="C#" ... %>

<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
    <h2> <%= Html.Encode(ViewData["Message"]) %></h2>
    <p>
        To learn more about ASP.NET MVC visit ...
    </p>

    <select id="Countries">
    <option>Switzerland</option>
    <option>United Kindom</option>
    <option>United States</option>
    </select>

</asp:Content>
 

Include the required Javascript files

Next you'll need to have the page pull in the JavaScript files that are used by the ListSearch Extender:

<script type="text/javascript" src="/Content/MicrosoftAjaxLibrary/System.Web.Extensions/3.5.0.0/3.5.21022.8/MicrosoftAjax.debug.js"/>
<script src="/Content/AjaxControlToolkit/3.0.20820.16598/3.0.20820.0/AjaxControlToolkit.Common.Common.js" type="text/javascript"></script>
<script src="/Content/AjaxControlToolkit/3.0.20820.16598/3.0.20820.0/AjaxControlToolkit.ExtenderBase.BaseScripts.js" type="text/javascript"></script>
<script src="/Content/AjaxControlToolkit/3.0.20820.16598/3.0.20820.0/AjaxControlToolkit.DynamicPopulate.DynamicPopulateBehavior.js" type="text/javascript"></script>
<script src="/Content/AjaxControlToolkit/3.0.20820.16598/3.0.20820.0/AjaxControlToolkit.Compat.Timer.Timer.js" type="text/javascript"></script>
<script src="/Content/AjaxControlToolkit/3.0.20820.16598/3.0.20820.0/AjaxControlToolkit.Animation.Animations.js" type="text/javascript"></script>
<script src="/Content/AjaxControlToolkit/3.0.20820.16598/3.0.20820.0/AjaxControlToolkit.Animation.AnimationBehavior.js" type="text/javascript"></script>
<script src="/Content/AjaxControlToolkit/3.0.20820.16598/3.0.20820.0/AjaxControlToolkit.PopupExtender.PopupBehavior.js" type="text/javascript"></script>
<script src="/Content/AjaxControlToolkit/3.0.20820.16598/3.0.20820.0/AjaxControlToolkit.PopupControl.PopupControlBehavior.js" type="text/javascript"></script>
<script src="/Content/AjaxControlToolkit/3.0.20820.16598/3.0.20820.0/AjaxControlToolkit.ListSearch.ListSearchBehavior.js" type="text/javascript"></script>

 
You may need to change the version numbers depending on the versions of the Microsoft AJAX library and Toolkits you downloaded.
 
I found out which script files to include by creating a normal ASP.NET Project that used the ListSearch Extender, and set the ScriptManager's ScriptPath property to a new folder I created in the project called /MicrosoftAjaxLibrary, to which I copied the same two folders that I copied to the Content folder above.  I accessed the page, and did a View Source to see what JavaScript was being used.
 

Initialize the Extender

Having included the required JavaScript files, you can now create a new ListSearch Extender and attach it to the SELECT you created above:

<script type="text/javascript">
    Sys.Application.initialize();
    Sys.Application.add_init(function() {
    $create(AjaxControlToolkit.ListSearchBehavior,
            { "id": "ListBox1_ListSearchExtender" },
            null, null, $get("Countries"));
    });
</script>
 

Access your page

 
Now when you run you'll get the ListSearch Extender behavior attached to your SELECT:
 

Summary

 
Using the AJAX Control Toolkit is very straight-forward.  The trickiest part is determining exactly what script files you need to include.
 
Now you can use the whole set of behaviors that other people have created to add AJAX goodness to your ASP.NET MVC projects.
Filed under: AJAX, MVC 2 Comments