Verifying validity of Workflow Service in Unit Test

Introduction

At work we’ve been having a little bit of trouble verifying that a Workflow Service still “works” after we change something. This is because our custom activities are in a separate assembly. When we change the activities and run MSBuild, we’ll notice that the unit tests still work and that the workflow still compiles. Only after we deploy the service and try to hit it, do we see errors that indicate that the workflow can not be instantiated, for instance because we removed a parameter from an activity. We need some kind of regression testing to prevent this.

To solve this, we now instantiate the workflow in a Unit Test as I will describe in this post.

Problem description

Suppose I have a very simple CodeActivity that takes two values and multiplies them:

public sealed class CodeActivity1 : CodeActivity
{
    public InArgument Input { get; set; }
    public InArgument Factor { get; set; }
    public OutArgument Result { get; set; }

    protected override void Execute(CodeActivityContext context)
    {
        var input = Input.Get(context);
        var factor = Factor.Get(context);

        var result = input*factor;

        Result.Set(context, result);
    }
}

I then have a Workflow Service which calls this activity with a value for Input that it gets from a service call, and a constant value of 3 for the Factor:

workflowservice1
Continue reading

Upgrade a SharePoint 2010 solution with features using Power Shell

Having finished the first three SharePoint 2010 portals with our team, we have come across the inevitable need to change certain things in an already running website. Since we obviously cannot retract, uninstall, reinstall and redeploy our solutions (the data would be lost), we have started looking into the upgrade process in SharePoint 2010. After having gained a lot of knowledge from this blog post by Chris O’Brien, we were ready to deploy our upgrades.

In his blog, Chris describes his SharePoint Feature Upgrade Kit, which provides a very handy way to graphically upgrade features, as well as to do the same by using Power Shell. However, even using the Power Shell version of his tools requires installing a few things to the GAC, which we would prefer not to have to ask our maintenance team. So we have set out to make some simple Power Shell scripts of our own which we can just put in a .ps1 file to give to our administrators. As Chris notes, there are no built-in commands to upgrade features easily, so we have to do this the ‘hard’ way ;)

This script will upgrade one SharePoint solution and then upgrade all of the features (both site and web scoped) it contains on the given SPSite.
Continue reading

link_to_unless_current without request parameters

Not a really big thing, but I felt the need for a method like link_to_unless_current which ignores request parameters (you know, those things that come after the ‘?’ in the url). I couldn’t find it in the Rails api, so I wrote my own little snippet in a helper:

  # makes a link unless 'link' equals the current
  # page ignoring request parameters
  #
  # e.g. this will not make a link:
  #   current page = http://your.web.site/articles?page=2
  #   link = http://your.web.site/articles
  # but this will:
  #   current page = http://your.web.site/articles/2
  #   link = http://your.web.site/articles
  def link_to_unless_current_ignore_parameters(title, link)
    link_to_unless(request.request_uri.gsub(/?.*/, '') == url_for(link), title, link)
  end

If someone knows a better way, please let me know :) I thought about aliasing the original method and having an optional boolean parameter that defines whether to ignore the request parameters, but decided to go with this instead.

Coding font roundup

As a followup of the poll about “What coding font do you use?” on Rails Envy, here are the results and a PDF showing all the fonts for comparison:

  1. Monaco (37%)
  2. Bitstream Vera Sans Mono (20%)
  3. Consolas (14%)
  4. Deja Vu (5%)
  5. Inconsolata (4%)
  6. Liberation Mono (2%)
  7. Pro Font (2%)
  8. Courier New (1.5%)
  9. Lucida Console (1%)
  10. Monospace (1%)

Some old favourites in there, but they seem to be going out of style in favor of the new ones. Consolas is one of the new fonts that ships with Windows Vista (or was it Office 2007?), Bitstream Vera Sans Mono is the font Ryan Bates at Railscasts uses and (I think) Monaco is the default font in Textmate.

Check out the pdf here. I use Consolas ;)

Update: I’ve heard that some users can’t view all the fonts in the PDF, so here’s a PNG of all the pages put together.

First tutorial: Writing a blog in Rails 2.0.2

I’ve just finished my first tutorial, writing a blog in Rails 2.0.2. It’s of course inspired by the great 15 minute blog screencast by DHH, which convinced me to try Rails in the first place. This one is with some new Rails 2 conventions and explanations on why some things work.

Now, without further ado, go and read it:

How to write a blog in Rails 2.0.2 (not quite in 15 minutes)

Comments and criticisms are always welcome, it’s my first tutorial ;)

Conditional fragment caching

We were just wondering if it’s possible to provide a condition to fragment caching, in order to only cache certain fragments when no user is logged in to our site. This is to ensure anonymous users (the majority of visitors), who will see the same page anyway, will get it loaded from cache. People who log in will see their personalized site.

I was unable to find anything on Google about this, so I wrote my own helper. It looks so obvious that I’m probably not the first to write this, but I figured I’d share it anyway:

def cache_if (condition, name = {}, &block)
  if condition
    cache(name, &block)
  else
    yield
  end
end

Using this, you can write something like:

<% cache_if @current_user.blank?, 'navigation' do %>
  Navigation content here, including user specific navigation
<% end %>

Loading additional files when starting script/console

I use the Rails console quite a lot, and from a co-worker I had received a .irbrc file that contains a method called ‘log_to’ which enables on-screen query logging in the Rails console. It still required me to call this method every time I started the console though, because even though it’s defined in .irbrc, it cannot be executed from there since it uses Rails specific code (which is not loaded at the time that the .irbrc is loaded.

So, naturally I was wondering whether it is possible to auto-execute any commands while starting script/console. It turns out it isn’t (according to the friendly people on IRC). With the help of madnificent though, I can now do it. He made a small change to rails/railties/lib/commands/console.rb, which will change the behaviour of the console such that it will look in #{RAILS_ROOT}/console_scripts for any additional Ruby files to load.

Please note, for this to work you will need to either freeze rails in your vendor directory, or change your local rails source. As I’m very new to Rails I’m not sure why this kind of functionality is not already in (or perhaps it is but I just could not find it), but I’m hoping it will be picked up somehow ;)

I have also included the script I wanted to load in the first place, I find it quite handy to be able to see what kind of queries my commands are generating.

You can find the files on Pastebin.